Simulation von Bürgern
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

UI.pde 7.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. Button buttons[] = new Button[4];
  2. int i_uiTransparency = 200;
  3. void initUI() {
  4. i_uiX = 0;
  5. i_uiY = 518;
  6. i_uiW = width;
  7. i_uiH = height - i_uiY;
  8. buttons[0] = new Button(15, 50, 100, i_debugTextSize, 5, "Autoplay", 0);
  9. buttons[1] = new Button(15, 70, 100, i_debugTextSize, 5, "Lock Camera", 1);
  10. buttons[2] = new Button(15, 90, 100, i_debugTextSize, 5, "Debug", 2);
  11. buttons[3] = new Button(15, 110, 100, i_debugTextSize, 5, "Control Eyes", 3);
  12. }
  13. int i_uiX, i_uiY, i_uiW, i_uiH;
  14. int i_uiOffsetX, i_uiOffsetY, i_uiOffsetStartX, i_uiOffsetStartY, i_initUiY;
  15. void drawUI() {
  16. //camera tracking
  17. if (b_cameraFollow) {
  18. if (b_mapZoom) {
  19. i_mapOffsetX = width/2-int(citizen[i_selectedCitizen].f_xPos)/2;
  20. i_mapOffsetY = height/2-int(citizen[i_selectedCitizen].f_yPos)/2;
  21. } else {
  22. i_mapOffsetX = width/2-int(citizen[i_selectedCitizen].f_xPos);
  23. i_mapOffsetY = height/2-int(citizen[i_selectedCitizen].f_yPos);
  24. }
  25. }
  26. //draw
  27. //buttons
  28. strokeWeight(0);
  29. textSize(i_debugTextSize);
  30. for (int i = 0; i < buttons.length; i++) {
  31. buttons[i].display();
  32. }
  33. //clock
  34. fill(255, i_uiTransparency);
  35. rect(6, 4, 140, 31, 6);
  36. textSize(30);
  37. textAlign(LEFT);
  38. fill(0);
  39. text(String.format("%02d", hour()) + ":" + String.format("%02d", minute()) + ":" + String.format("%02d", second()), 10, 30);
  40. //debug
  41. if (b_debug) {
  42. //bottom
  43. fill(255, i_uiTransparency);
  44. strokeWeight(1);
  45. line(i_uiX, i_uiY, i_uiX+i_uiW, i_uiY);
  46. strokeWeight(0);
  47. rect(i_uiX, i_uiY, i_uiW, i_uiH);
  48. fill(0);
  49. String[] debug = {
  50. "FPS: " + int(frameRate),
  51. "Amount citizen: " + i_citizenAmount,
  52. "Females: " + i_femaleCount + " Males: " + str(i_citizenAmount-i_femaleCount),
  53. " ",
  54. "Selected Citizen ID: " + i_selectedCitizen,
  55. "Name: " + citizen[i_selectedCitizen].s_name,
  56. "Gender: " + citizen[i_selectedCitizen].s_gender,
  57. "Age: " + citizen[i_selectedCitizen].i_age,
  58. "x: " + int(citizen[i_selectedCitizen].f_xPos),
  59. "y: " + int(citizen[i_selectedCitizen].f_yPos),
  60. "Angle: " + citizen[i_selectedCitizen].f_angle,
  61. "Movement speed: " + round(citizen[i_selectedCitizen].f_movementSpeed*100),
  62. "Random Run: " + citizen[i_selectedCitizen].b_randomRun,
  63. "Turning: " + citizen[i_selectedCitizen].b_turning,
  64. " ",
  65. "Sunrise: " + S_sunrise,
  66. "Sunset: " + S_sunset,
  67. "Autoplay: " + b_autoPlayAfterSelect,
  68. "Locked Camera: " + b_cameraFollow,
  69. "Debug Mode: " + b_debug,
  70. //"Velocity: ",
  71. //"Outside",
  72. //"Mood: "
  73. };
  74. textSize(i_debugTextSize);
  75. int line = 0;
  76. int maxLinesPerColumn = i_uiH/i_debugTextSize;
  77. int columnWidth = 130;
  78. if (maxLinesPerColumn > 0) {
  79. int requiredColumns = (debug.length/maxLinesPerColumn)+1;
  80. for (int i = 0; i < requiredColumns; i++) {
  81. while (((line-(maxLinesPerColumn*i)) * i_debugTextSize + i_debugTextSize -1)+i_uiY<height && line < debug.length) {
  82. text(debug[line], 10+(columnWidth*i), i_uiY+(i_debugTextSize*((line-(maxLinesPerColumn*i))+1)));
  83. line++;
  84. }
  85. }
  86. }
  87. }
  88. }
  89. void buttonFunctions(int s) {
  90. switch (s) {
  91. case(0):
  92. citizen[i_selectedCitizen].b_moving = !citizen[i_selectedCitizen].b_moving;
  93. citizen[i_selectedCitizen].b_randomRun = !citizen[i_selectedCitizen].b_randomRun;
  94. b_autoPlayAfterSelect = !b_autoPlayAfterSelect;
  95. break;
  96. case(1):
  97. b_cameraFollow = !b_cameraFollow;
  98. break;
  99. case(2):
  100. b_debug = !b_debug;
  101. //dirty
  102. for(int i = 0; i < citizen.length; i++){
  103. citizen[i].c_stroke = color(int(!b_debug)*255);
  104. }
  105. for(int i = 0; i < interactables.length; i++){
  106. interactables[i].c_stroke = color(int(!b_debug)*255);
  107. }
  108. break;
  109. case(3):
  110. b_controlEyeWithMouse = !b_controlEyeWithMouse;
  111. break;
  112. default:
  113. break;
  114. }
  115. }
  116. class Button {
  117. int i_x, i_y, i_w, i_h, i_c, i_f, i_tx;
  118. int i_margin = 5;
  119. String s_t;
  120. Button(int x, int y, int w, int h, int corner, String text, int function) {
  121. i_x = x;
  122. i_y = y;
  123. i_w = w;
  124. i_h = h;
  125. i_c = corner;
  126. i_f = function;
  127. s_t = text;
  128. i_tx = i_x + (i_w/2) - (int(textWidth(s_t) /2));
  129. }
  130. boolean hover() {
  131. if (mouseX > i_x - i_margin && mouseX < i_x+i_w + i_margin && mouseY > i_y - i_margin && mouseY < i_y+i_h + i_margin) return true;
  132. else return false;
  133. }
  134. void click() {
  135. buttonFunctions(i_f);
  136. }
  137. void display() {
  138. fill(150, 170, 250, 150);
  139. rect(i_x, i_y, i_w, i_h, i_c);
  140. fill(0);
  141. text(s_t, i_tx, i_y + i_debugTextSize);
  142. }
  143. }
  144. boolean b_mouseChangeMapOffset, b_mouseChangeUiHeight;
  145. boolean b_hoverUI;
  146. void mousePressed() {
  147. if (mouseButton == LEFT) {
  148. boolean clickUI = false;
  149. if (mouseX >= i_uiX && mouseX <= i_uiX+i_uiW && mouseY >= i_uiY-10 && mouseY <= i_uiY+10) { //drag and drop debug window size
  150. b_mouseChangeUiHeight = true;
  151. i_uiOffsetStartY = mouseY;
  152. i_initUiY = i_uiY;
  153. clickUI = true;
  154. }
  155. for (int i = 0; i < buttons.length; i++) {
  156. if (buttons[i].hover()) {
  157. buttons[i].click();
  158. clickUI = true;
  159. }
  160. }
  161. if (!clickUI) {
  162. int selectedCitizen = i_selectedCitizen; //leftclick select citizen
  163. for (int i = 0; i < citizen.length; i++) {
  164. if (selectedCitizen != i) {
  165. if (i_mapViewToMap(mouseX, i_mapOffsetX)+10 >= citizen[i].f_xPos - citizen[i].i_diameter/2 && i_mapViewToMap(mouseX, i_mapOffsetX)-10 <= citizen[i].f_xPos + citizen[i].i_diameter/2
  166. && i_mapViewToMap(mouseY, i_mapOffsetY)+10 >= citizen[i].f_yPos - citizen[i].i_diameter/2 && i_mapViewToMap(mouseY, i_mapOffsetY)-10 <= citizen[i].f_yPos + citizen[i].i_diameter/2) {
  167. selectCitizen(i);
  168. }
  169. }
  170. }
  171. }
  172. }
  173. if (mouseButton == CENTER) {
  174. if (!b_cameraFollow) {
  175. b_mouseChangeMapOffset = true;
  176. i_mapOffsetStartX = mouseX - i_mapOffsetX;
  177. i_mapOffsetStartY = mouseY - i_mapOffsetY;
  178. }
  179. }
  180. if (mouseButton == RIGHT) {
  181. // if(citizen[i_selectedCitizen].hover){
  182. //}
  183. if (!b_autoPlayAfterSelect) {
  184. citizen[i_selectedCitizen].b_randomRun = false;
  185. citizen[i_selectedCitizen].b_moving = false;
  186. }
  187. citizen[i_selectedCitizen].goTo(constrain(i_mapViewToMap(mouseX, i_mapOffsetX), 0, pg_map.width), constrain(i_mapViewToMap(mouseY, i_mapOffsetY), 0, pg_map.height), 10);
  188. citizen[i_selectedCitizen].turnTo(citizen[i_selectedCitizen].f_xPos-constrain(i_mapViewToMap(mouseX, i_mapOffsetX), 0, pg_map.width), citizen[i_selectedCitizen].f_yPos-constrain(i_mapViewToMap(mouseY, i_mapOffsetY), 0, pg_map.height),4);
  189. }
  190. }
  191. int i_mapViewToMap(int mouse, int offset) {
  192. if (b_mapZoom) return (mouse-offset)*2;
  193. else return mouse-offset;
  194. }
  195. void mouseDragged() {
  196. if (b_mouseChangeMapOffset) {
  197. i_mapOffsetX = (mouseX - i_mapOffsetStartX);
  198. i_mapOffsetY = (mouseY - i_mapOffsetStartY);
  199. }
  200. if (b_mouseChangeUiHeight) {
  201. i_uiY = mouseY - i_uiOffsetStartY + i_initUiY;
  202. if (i_uiY > height) i_uiY = height;
  203. i_uiH = height - i_uiY;
  204. //hook buttons to ui panel
  205. }
  206. }
  207. void mouseReleased() {
  208. b_mouseChangeMapOffset = false;
  209. b_mouseChangeUiHeight = false;
  210. }
  211. //bad
  212. //int maxScale = 5;
  213. //float zoomFactor = 0.4;
  214. //int i_imgCenterX;
  215. //int i_imgCenterY;
  216. int i_mapScale = 1;
  217. boolean b_mapZoom;
  218. float e;
  219. void mouseWheel(MouseEvent event) {
  220. e = event.getAmount();
  221. if (e == -1) {
  222. i_mapScale+=1;
  223. }
  224. if (e == 1) {
  225. i_mapScale-=1;
  226. }
  227. constrain(i_mapScale, 0, 2);
  228. switch(int(e)) {
  229. case 1:
  230. if (!b_mapZoom) {
  231. i_mapW = pg_map.width/2;
  232. i_mapH = pg_map.height/2;
  233. i_mapOffsetX -= 3*i_mapOffsetX/4;
  234. i_mapOffsetY -= 3*i_mapOffsetY/4;
  235. b_mapZoom = true;
  236. }
  237. break;
  238. case -1:
  239. i_mapW = pg_map.width;
  240. i_mapH = pg_map.height;
  241. b_mapZoom = false;
  242. break;
  243. }
  244. }