comparison game/IDMWidget.java @ 162:e8eeac403e5f

Backed out changeset fb33d3796942
author Matti Hamalainen <ccr@tnsp.org>
date Thu, 01 Dec 2016 14:33:25 +0200
parents src/IDMWidget.java@fb33d3796942
children b9bc493ae53c
comparison
equal deleted inserted replaced
161:fb33d3796942 162:e8eeac403e5f
1 /*
2 * Ristipolku IDM base widget
3 * (C) Copyright 2011 Matti 'ccr' Hämäläinen <ccr@tnsp.org>
4 */
5 package game;
6
7 import java.awt.*;
8 import java.awt.event.*;
9
10
11 public class IDMWidget
12 {
13 IDMWidget parent;
14 IDMPoint pos, size, scale;
15 int keyCode;
16
17 public IDMWidget()
18 {
19 keyCode = -1;
20 this.scale = new IDMPoint(1, 1);
21 this.pos = new IDMPoint(0, 0);
22 this.size = new IDMPoint(0, 0);
23 }
24
25 public IDMWidget(IDMPoint pos)
26 {
27 this();
28 this.pos = pos;
29 }
30
31 public IDMWidget(IDMPoint pos, IDMPoint size)
32 {
33 this();
34 this.pos = pos;
35 this.size = size;
36 }
37
38 public void setParent(IDMWidget par)
39 {
40 this.parent = par;
41 }
42
43 public void add(IDMWidget widget)
44 {
45 }
46
47 public void remove(IDMWidget widget)
48 {
49 }
50
51 public void setPos(IDMPoint pos)
52 {
53 this.pos = pos;
54 }
55
56 public void setPos(float x, float y)
57 {
58 this.pos = new IDMPoint(x, y);
59 }
60
61 public void setSize(IDMPoint size)
62 {
63 this.size = size;
64 }
65
66 public void setSize(float w, float h)
67 {
68 this.size = new IDMPoint(w, h);
69 }
70
71 public void setScale(IDMPoint scale)
72 {
73 this.scale = scale;
74 }
75
76 public void setScale(float x, float y)
77 {
78 this.setScale(new IDMPoint(x, y));
79 }
80
81 public int getScaledX()
82 {
83 return (int) (pos.x * scale.x);
84 }
85
86 public int getScaledY()
87 {
88 return (int) (pos.y * scale.y);
89 }
90
91 public int getScaledWidth()
92 {
93 return (int) (size.x * scale.x);
94 }
95
96 public int getScaledHeight()
97 {
98 return (int) (size.y * scale.y);
99 }
100
101 public boolean contains(float x, float y)
102 {
103 return (x >= getScaledX() &&
104 y >= getScaledY() &&
105 x < getScaledX() + getScaledWidth() &&
106 y < getScaledY() + getScaledHeight());
107 }
108
109 public boolean contains(Point where)
110 {
111 return contains(where.x, where.y);
112 }
113
114 public boolean contains(IDMPoint where)
115 {
116 return contains(where.x, where.y);
117 }
118
119 public void paint(Graphics2D g)
120 {
121 }
122
123 public boolean mousePressed(MouseEvent e)
124 {
125 return false;
126 }
127
128 public boolean mouseReleased(MouseEvent e)
129 {
130 if (contains(e.getPoint()))
131 {
132 clicked();
133 return true;
134 }
135 return false;
136 }
137
138 public boolean mouseEntered(MouseEvent e)
139 {
140 return false;
141 }
142
143 public boolean mouseExited(MouseEvent e)
144 {
145 return false;
146 }
147
148 // Generic key handler
149 public boolean keyPressed(KeyEvent e)
150 {
151 if (e.getKeyCode() == keyCode)
152 {
153 clicked();
154 return true;
155 }
156 else
157 return false;
158 }
159
160 public void clicked()
161 {
162 }
163 }