From: Miguel A. B. L. <ari...@us...> - 2005-10-18 12:28:21
|
Update of /cvsroot/arianne/stendhal/src/games/stendhal/client/gui In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv25519/src/games/stendhal/client/gui Modified Files: InGameGUI.java Log Message: Refactored (1st part) InGameGUI. Splitted the class on smaller simpler classes. Index: InGameGUI.java =================================================================== RCS file: /cvsroot/arianne/stendhal/src/games/stendhal/client/gui/InGameGUI.java,v retrieving revision 1.37 retrieving revision 1.38 diff -C2 -d -r1.37 -r1.38 *** InGameGUI.java 15 Oct 2005 16:43:39 -0000 1.37 --- InGameGUI.java 18 Oct 2005 12:28:12 -0000 1.38 *************** *** 14,29 **** import games.stendhal.client.*; ! import games.stendhal.client.entity.Entity; ! import games.stendhal.client.entity.PassiveEntity; import games.stendhal.common.Direction; import java.awt.*; import java.awt.event.*; import java.awt.geom.Point2D; import java.util.HashMap; import java.util.Map; import marauroa.common.Log4J; import marauroa.common.game.RPAction; import marauroa.common.game.RPObject; import marauroa.common.game.RPSlot; import org.apache.log4j.Logger; --- 14,33 ---- import games.stendhal.client.*; ! import games.stendhal.client.entity.*; ! import games.stendhal.client.gui.wt.*; import games.stendhal.common.Direction; + import java.awt.*; import java.awt.event.*; import java.awt.geom.Point2D; + import java.util.HashMap; import java.util.Map; + import marauroa.common.Log4J; import marauroa.common.game.RPAction; import marauroa.common.game.RPObject; import marauroa.common.game.RPSlot; + import org.apache.log4j.Logger; *************** *** 36,306 **** private static final Logger logger = Log4J.getLogger(InGameGUI.class); ! interface InGameAction ! { ! public void onAction(Object... param); ! } ! ! abstract class InGameActionListener implements InGameAction ! { ! abstract public void onAction(Object... param); ! } ! ! static class InGameButton ! { ! private String name; ! private Sprite[] buttons; ! private Rectangle area; ! private InGameAction action; ! private boolean over; ! private boolean enabled; ! ! public InGameButton(String name, Sprite normal, Sprite over, int x, int y) ! { ! buttons=new Sprite[2]; ! buttons[0]=normal; ! buttons[1]=over; ! ! area=new Rectangle(x,y,buttons[0].getWidth(),buttons[0].getHeight()); ! this.over=false; ! this.action=null; ! this.enabled=true; ! this.name=name; ! } ! ! public String getName() ! { ! return name; ! } ! ! public void setEnabled(boolean enabled) ! { ! this.enabled=enabled; ! } ! ! public void draw(GameScreen screen) ! { ! if(!enabled) return; ! Sprite button; ! ! if(over) ! { ! button=buttons[1]; ! } ! else ! { ! button=buttons[0]; ! } ! ! screen.drawInScreen(button,(int)area.getX(),(int)area.getY()); ! } ! ! public void addActionListener(InGameAction action) ! { ! this.action=action; ! } ! ! public boolean onMouseOver(Point2D point) ! { ! if(!enabled) return false; ! if(area.contains(point)) ! { ! over=true; ! } ! else ! { ! over=false; ! } ! ! return false; ! } ! ! public boolean clicked(Point2D point) ! { ! if(!enabled) return false; ! if(area.contains(point)) ! { ! action.onAction(); ! return true; ! } ! ! return false; ! } ! } ! ! static class InGameDroppableArea ! { ! private String name; ! private Rectangle area; ! private InGameAction action; ! private boolean enabled; ! ! public InGameDroppableArea(String name,int x, int y, int width, int height) ! { ! this.name=name; ! area=new Rectangle(x,y,width,height); ! this.action=null; ! this.enabled=true; ! } ! ! public String getName() ! { ! return name; ! } ! ! public int getx() ! { ! return (int)area.getX(); ! } ! ! public int gety() ! { ! return (int)area.getY(); ! } ! ! public void setEnabled(boolean enabled) ! { ! this.enabled=enabled; ! } ! ! public void addActionListener(InGameAction action) ! { ! this.action=action; ! } ! ! public void draw(GameScreen screen) ! { ! if(!enabled) return; ! Graphics g=screen.expose(); ! g.setColor(Color.white); ! g.drawRect((int)area.getX(),(int)area.getY(),(int)area.getWidth(),(int)area.getHeight()); ! } ! ! public boolean isMouseOver(Point2D point) ! { ! if(!enabled) return false; ! if(area.contains(point)) ! { ! return true; ! } ! ! return false; ! } ! ! public boolean released(Point2D point, Entity choosenEntity) ! { ! if(!enabled) return false; ! if(area.contains(point)) ! { ! action.onAction(choosenEntity,this); ! return true; ! } ! ! return false; ! } ! ! public boolean released(Point2D point, InGameDroppableArea choosenWidget) ! { ! if(!enabled) return false; ! if(area.contains(point)) ! { ! action.onAction(choosenWidget,this); ! return true; ! } ! ! return false; ! } ! } ! ! static class InGameList ! { ! private Rectangle area; ! private String[] list; ! private int choosen; ! private int over; ! private Sprite action_list; ! ! private Sprite render(double x, double y, double mouse_x, double mouse_y) ! { ! int width=70+6; ! int height=6+16*list.length; ! ! area= new Rectangle((int)x,(int)y,width,height); ! GraphicsConfiguration gc = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().getDefaultConfiguration(); ! Image image = gc.createCompatibleImage(width,height,Transparency.BITMASK); ! Graphics g=image.getGraphics(); ! ! g.setColor(Color.gray); ! g.fillRect(0,0,width,height); ! ! g.setColor(Color.black); ! g.drawRect(0,0,width-1,height-1); ! ! g.setColor(Color.yellow); ! int i=0; ! for(String item: list) ! { ! if((mouse_y-y)>16*i && (mouse_y-y)<16*(i+1)) ! { ! g.setColor(Color.white); ! g.drawRect(0,16*i,width-1,16); ! g.drawString(item,3,13+16*i); ! g.setColor(Color.yellow); ! over=i; ! } ! else ! { ! g.drawString(item,3,13+16*i); ! } ! ! i++; ! } ! ! return new Sprite(image); ! } ! ! public InGameList(String[] list, double x, double y) ! { ! this.list=list; ! over=-1; ! action_list=render(x,y,-1,-1); ! } ! ! public void draw(GameScreen screen) ! { ! Point2D translated=screen.translate(new Point((int)area.getX(),(int)area.getY())); ! screen.draw(action_list,translated.getX(),translated.getY()); ! } ! ! public boolean onMouseOver(Point2D point) ! { ! if(area.contains(point) && over!=(point.getY()-area.getY())/16) ! { ! action_list=render(area.getX(),area.getY(),point.getX(),point.getY()); ! return true; ! } ! ! return false; ! } ! ! public boolean clicked(Point2D point) ! { ! if(area.contains(point)) ! { ! choosen=(int)((point.getY()-area.getY())/16); ! return true; ! } ! ! return false; ! } ! ! public String choosen() ! { ! return list[choosen]; ! } ! } ! ! private InGameList widget; ! private java.util.List<InGameButton> buttons; ! private java.util.List<InGameDroppableArea> droppableAreas; private Entity widgetAssociatedEntity; --- 40,46 ---- private static final Logger logger = Log4J.getLogger(InGameGUI.class); ! private wtList widget; ! private java.util.List<wtButton> buttons; ! private java.util.List<wtDroppableArea> droppableAreas; private Entity widgetAssociatedEntity; *************** *** 316,320 **** private RPSlot inspectedSlot; ! private java.util.List<InGameDroppableArea> inspectedDroppableAreas; private Entity inspectedEntity; --- 56,60 ---- private RPSlot inspectedSlot; ! private java.util.List<wtDroppableArea> inspectedDroppableAreas; private Entity inspectedEntity; *************** *** 358,364 **** pressed=new HashMap<Integer, Object>(); ! buttons=new java.util.LinkedList<InGameButton>(); ! droppableAreas=new java.util.LinkedList<InGameDroppableArea>(); ! inspectedDroppableAreas=new java.util.LinkedList<InGameDroppableArea>(); buildGUI(); --- 98,104 ---- pressed=new HashMap<Integer, Object>(); ! buttons=new java.util.LinkedList<wtButton>(); ! droppableAreas=new java.util.LinkedList<wtDroppableArea>(); ! inspectedDroppableAreas=new java.util.LinkedList<wtDroppableArea>(); buildGUI(); *************** *** 369,374 **** SpriteStore st=SpriteStore.get(); ! InGameButton button=new InGameButton("exit",st.getSprite("data/exit.png"), st.getSprite("data/exit_pressed.png"), 320,360); ! button.addActionListener(new InGameActionListener() { public void onAction(Object... param) --- 109,114 ---- SpriteStore st=SpriteStore.get(); ! wtButton button=new wtButton("exit",st.getSprite("data/exit.png"), st.getSprite("data/exit_pressed.png"), 320,360); ! button.addActionListener(new wtEventListener() { public void onAction(Object... param) *************** *** 380,389 **** buttons.add(button); ! button=new InGameButton("back",st.getSprite("data/back.png"), st.getSprite("data/back_pressed.png"), 220,360); ! button.addActionListener(new InGameActionListener() { public void onAction(Object... param) { ! for(InGameButton button: buttons) { if(button.getName().equals("exit") || button.getName().equals("back")) --- 120,129 ---- buttons.add(button); ! button=new wtButton("back",st.getSprite("data/back.png"), st.getSprite("data/back_pressed.png"), 220,360); ! button.addActionListener(new wtEventListener() { public void onAction(Object... param) { ! for(wtButton button: buttons) { if(button.getName().equals("exit") || button.getName().equals("back")) *************** *** 400,404 **** inGameInventory=SpriteStore.get().getSprite("data/equipmentGUI.png",true); ! InGameActionListener dropToInventory=new InGameActionListener() { public void onAction(Object... param) --- 140,144 ---- inGameInventory=SpriteStore.get().getSprite("data/equipmentGUI.png",true); ! wtEventListener dropToInventory=new wtEventListener() { public void onAction(Object... param) *************** *** 410,425 **** action.put("type","equip"); action.put("target",((Entity)param[0]).getID().getObjectID()); ! action.put("slot",((InGameDroppableArea)param[1]).getName()); action.put("baseobject",playerid); InGameGUI.this.client.send(action); } ! else if(param[0] instanceof InGameDroppableArea) { RPAction action=new RPAction(); action.put("type","moveequip"); ! action.put("targetslot",((InGameDroppableArea)param[1]).getName()); action.put("targetobject",playerid); ! action.put("sourceslot",((InGameDroppableArea)param[0]).getName()); action.put("sourceobject",playerid); InGameGUI.this.client.send(action); --- 150,165 ---- action.put("type","equip"); action.put("target",((Entity)param[0]).getID().getObjectID()); ! action.put("slot",((wtDroppableArea)param[1]).getName()); action.put("baseobject",playerid); InGameGUI.this.client.send(action); } ! else if(param[0] instanceof wtDroppableArea) { RPAction action=new RPAction(); action.put("type","moveequip"); ! action.put("targetslot",((wtDroppableArea)param[1]).getName()); action.put("targetobject",playerid); ! action.put("sourceslot",((wtDroppableArea)param[0]).getName()); action.put("sourceobject",playerid); InGameGUI.this.client.send(action); *************** *** 428,458 **** }; ! InGameDroppableArea area=null; ! area=new InGameDroppableArea("lhand",515,68,32,32); area.addActionListener(dropToInventory); droppableAreas.add(area); ! // area=new InGameDroppableArea("head",558,14,32,32); // area.addActionListener(dropToInventory); // droppableAreas.add(area); ! area=new InGameDroppableArea("armor",558,56,32,32); area.addActionListener(dropToInventory); droppableAreas.add(area); ! // area=new InGameDroppableArea("legs",558,98,32,32); // area.addActionListener(dropToInventory); // droppableAreas.add(area); // ! // area=new InGameDroppableArea("feet",558,141,32,32); // area.addActionListener(dropToInventory); // droppableAreas.add(area); ! area=new InGameDroppableArea("rhand",601,68,32,32); area.addActionListener(dropToInventory); droppableAreas.add(area); ! area=new InGameDroppableArea("bag",601,122,32,32); area.addActionListener(dropToInventory); droppableAreas.add(area); --- 168,198 ---- }; ! wtDroppableArea area=null; ! area=new wtDroppableArea("lhand",515,68,32,32); area.addActionListener(dropToInventory); droppableAreas.add(area); ! // area=new wtDroppableArea("head",558,14,32,32); // area.addActionListener(dropToInventory); // droppableAreas.add(area); ! area=new wtDroppableArea("armor",558,56,32,32); area.addActionListener(dropToInventory); droppableAreas.add(area); ! // area=new wtDroppableArea("legs",558,98,32,32); // area.addActionListener(dropToInventory); // droppableAreas.add(area); // ! // area=new wtDroppableArea("feet",558,141,32,32); // area.addActionListener(dropToInventory); // droppableAreas.add(area); ! area=new wtDroppableArea("rhand",601,68,32,32); area.addActionListener(dropToInventory); droppableAreas.add(area); ! area=new wtDroppableArea("bag",601,122,32,32); area.addActionListener(dropToInventory); droppableAreas.add(area); *************** *** 460,464 **** slot=st.getSprite("data/slot.png"); ! InGameActionListener transferFromContainer=new InGameActionListener() { public void onAction(Object... param) --- 200,204 ---- slot=st.getSprite("data/slot.png"); ! wtEventListener transferFromContainer=new wtEventListener() { public void onAction(Object... param) *************** *** 470,482 **** action.put("type","equip"); action.put("target",((Entity)param[0]).getID().getObjectID()); ! action.put("slot",((InGameDroppableArea)param[1]).getName()); InGameGUI.this.client.send(action); } ! else if(param[0] instanceof InGameDroppableArea) { RPAction action=new RPAction(); action.put("type","moveequip"); ! action.put("targetslot",((InGameDroppableArea)param[1]).getName()); ! action.put("sourceslot",((InGameDroppableArea)param[0]).getName()); InGameGUI.this.client.send(action); } --- 210,222 ---- action.put("type","equip"); action.put("target",((Entity)param[0]).getID().getObjectID()); ! action.put("slot",((wtDroppableArea)param[1]).getName()); InGameGUI.this.client.send(action); } ! else if(param[0] instanceof wtDroppableArea) { RPAction action=new RPAction(); action.put("type","moveequip"); ! action.put("targetslot",((wtDroppableArea)param[1]).getName()); ! action.put("sourceslot",((wtDroppableArea)param[0]).getName()); InGameGUI.this.client.send(action); } *************** *** 484,508 **** }; ! area=new InGameDroppableArea("left_001",6,414,32,32); area.addActionListener(transferFromContainer); inspectedDroppableAreas.add(area); droppableAreas.add(area); ! area=new InGameDroppableArea("left_002",6,369,32,32); area.addActionListener(transferFromContainer); inspectedDroppableAreas.add(area); droppableAreas.add(area); ! area=new InGameDroppableArea("left_003",6,324,32,32); area.addActionListener(transferFromContainer); inspectedDroppableAreas.add(area); droppableAreas.add(area); ! area=new InGameDroppableArea("left_004",6,279,32,32); area.addActionListener(transferFromContainer); inspectedDroppableAreas.add(area); droppableAreas.add(area); ! for(InGameDroppableArea disabledArea:inspectedDroppableAreas) { disabledArea.setEnabled(false); --- 224,248 ---- }; ! area=new wtDroppableArea("left_001",6,414,32,32); area.addActionListener(transferFromContainer); inspectedDroppableAreas.add(area); droppableAreas.add(area); ! area=new wtDroppableArea("left_002",6,369,32,32); area.addActionListener(transferFromContainer); inspectedDroppableAreas.add(area); droppableAreas.add(area); ! area=new wtDroppableArea("left_003",6,324,32,32); area.addActionListener(transferFromContainer); inspectedDroppableAreas.add(area); droppableAreas.add(area); ! area=new wtDroppableArea("left_004",6,279,32,32); area.addActionListener(transferFromContainer); inspectedDroppableAreas.add(area); droppableAreas.add(area); ! for(wtDroppableArea disabledArea:inspectedDroppableAreas) { disabledArea.setEnabled(false); *************** *** 510,516 **** } ! private InGameDroppableArea getDroppableArea(String name) { ! for(InGameDroppableArea item: droppableAreas) { if(item.getName().equals(name)) --- 250,256 ---- } ! private wtDroppableArea getDroppableArea(String name) { ! for(wtDroppableArea item: droppableAreas) { if(item.getName().equals(name)) *************** *** 525,529 **** private MouseEvent lastDraggedEvent; private Entity choosenEntity; ! private InGameDroppableArea choosenWidget; public void mouseDragged(MouseEvent e) --- 265,269 ---- private MouseEvent lastDraggedEvent; private Entity choosenEntity; ! private wtDroppableArea choosenWidget; public void mouseDragged(MouseEvent e) *************** *** 541,545 **** } ! for(InGameButton button: buttons) { button.onMouseOver(e.getPoint()); --- 281,285 ---- } ! for(wtButton button: buttons) { button.onMouseOver(e.getPoint()); *************** *** 563,567 **** // check if someone clicked the 'quit' or 'cancel' button ! for(InGameButton button: buttons) { button.clicked(e.getPoint()); --- 303,307 ---- // check if someone clicked the 'quit' or 'cancel' button ! for(wtButton button: buttons) { button.clicked(e.getPoint()); *************** *** 586,590 **** // ... show context menu String[] actions=entity.offeredActions(); ! widget=new InGameList(actions,screenPoint.getX(),screenPoint.getY()); widgetAssociatedEntity=entity; } --- 326,330 ---- // ... show context menu String[] actions=entity.offeredActions(); ! widget=new wtList(actions,screenPoint.getX(),screenPoint.getY()); widgetAssociatedEntity=entity; } *************** *** 602,606 **** if(choosenEntity==null) { ! for(InGameDroppableArea item: droppableAreas) { if(item.isMouseOver(e.getPoint())) --- 342,346 ---- if(choosenEntity==null) { ! for(wtDroppableArea item: droppableAreas) { if(item.isMouseOver(e.getPoint())) *************** *** 623,627 **** // We check first inventory and if it fails we wanted to move the object so. ! for(InGameDroppableArea item: droppableAreas) { if(item.released(e.getPoint(),choosenEntity)) --- 363,367 ---- // We check first inventory and if it fails we wanted to move the object so. ! for(wtDroppableArea item: droppableAreas) { if(item.released(e.getPoint(),choosenEntity)) *************** *** 646,650 **** // We check first inventory and if it fails we wanted to move the object so. ! for(InGameDroppableArea item: droppableAreas) { if(item.released(e.getPoint(),choosenWidget)) --- 386,390 ---- // We check first inventory and if it fails we wanted to move the object so. ! for(wtDroppableArea item: droppableAreas) { if(item.released(e.getPoint(),choosenWidget)) *************** *** 804,808 **** client.send(rpaction); ! for(InGameButton button: buttons) { if(button.getName().equals("exit") || button.getName().equals("back")) --- 544,548 ---- client.send(rpaction); ! for(wtButton button: buttons) { if(button.getName().equals("exit") || button.getName().equals("back")) *************** *** 816,820 **** public void inspect(Entity entity, RPSlot slot) { ! for(InGameDroppableArea area:inspectedDroppableAreas) { area.setEnabled(entity!=null); --- 556,560 ---- public void inspect(Entity entity, RPSlot slot) { ! for(wtDroppableArea area:inspectedDroppableAreas) { area.setEnabled(entity!=null); *************** *** 846,850 **** screen.drawInScreen(inGameInventory,510,10); ! for(InGameDroppableArea item: droppableAreas) { item.draw(screen); --- 586,590 ---- screen.drawInScreen(inGameInventory,510,10); ! for(wtDroppableArea item: droppableAreas) { item.draw(screen); *************** *** 862,866 **** if(slot.size()==1) { ! InGameDroppableArea dropArea=getDroppableArea(slotName); RPObject object=slot.iterator().next(); screen.drawInScreen(gameObjects.spriteType(object),dropArea.getx(),dropArea.gety()); --- 602,606 ---- if(slot.size()==1) { ! wtDroppableArea dropArea=getDroppableArea(slotName); RPObject object=slot.iterator().next(); screen.drawInScreen(gameObjects.spriteType(object),dropArea.getx(),dropArea.gety()); *************** *** 879,883 **** for(RPObject object: inspectedSlot) { ! InGameDroppableArea area=getDroppableArea("left_00"+i); screen.drawInScreen(slot,area.getx()-4,area.gety()-4); screen.drawInScreen(gameObjects.spriteType(object),area.getx(),area.gety()); --- 619,623 ---- for(RPObject object: inspectedSlot) { ! wtDroppableArea area=getDroppableArea("left_00"+i); screen.drawInScreen(slot,area.getx()-4,area.gety()-4); screen.drawInScreen(gameObjects.spriteType(object),area.getx(),area.gety()); *************** *** 887,891 **** if(inspectedEntity.distance(player)>2.5*2.5) { ! for(InGameDroppableArea area:inspectedDroppableAreas) { area.setEnabled(false); --- 627,631 ---- if(inspectedEntity.distance(player)>2.5*2.5) { ! for(wtDroppableArea area:inspectedDroppableAreas) { area.setEnabled(false); *************** *** 898,907 **** } ! for(InGameDroppableArea item: droppableAreas) { item.draw(screen); } ! for(InGameButton button: buttons) { button.draw(screen); --- 638,647 ---- } ! for(wtDroppableArea item: droppableAreas) { item.draw(screen); } ! for(wtButton button: buttons) { button.draw(screen); |