/** * @brief Creates a new system. */ static void uniedit_newSys( double x, double y ) { char *name; StarSystem *sys; /* Get name. */ name = dialogue_inputRaw( "New Star System Creation", 1, 32, "What do you want to name the new system?" ); /* Abort. */ if (name == NULL) { dialogue_alert( "Star System creation aborted!" ); return; } /* Make sure there is no collision. */ if (uniedit_checkName( name )) { free(name); uniedit_newSys( x, y ); return; } /* Create the system. */ sys = system_new(); sys->name = name; sys->pos.x = x; sys->pos.y = y; sys->stars = 400; sys->radius = 10000.; /* Select new system. */ uniedit_deselect(); uniedit_selectAdd( sys ); }
/** * @brief Closes the system editor widget. */ static void uniedit_close( unsigned int wid, char *wgt ) { /* Frees some memory. */ uniedit_deselect(); /* Reconstruct jumps. */ systems_reconstructJumps(); /* Close the window. */ window_close( wid, wgt ); }
/** * @brief Sets the selected system text. */ static void uniedit_selectText (void) { int i, l; char buf[1024]; StarSystem *sys; int hasPresence; l = 0; for (i=0; i<uniedit_nsys; i++) { l += nsnprintf( &buf[l], sizeof(buf)-l, "%s%s", uniedit_sys[i]->name, (i == uniedit_nsys-1) ? "" : ", " ); } if (l == 0) uniedit_deselect(); else { window_modifyText( uniedit_wid, "txtSelected", buf ); /* Presence text. */ if (uniedit_nsys == 1) { sys = uniedit_sys[0]; buf[0] = '\0'; hasPresence = 0; l = 0; for (i=0; i < sys->npresence ; i++) { /* Must have presence. */ if (sys->presence[i].value <= 0) continue; hasPresence = 1; /* Use map grey instead of default neutral colour */ l += nsnprintf( &buf[l], sizeof(buf)-l, "%s\e0%s: %.0f", (l==0)?"":"\n", faction_name(sys->presence[i].faction), sys->presence[i].value); } if (hasPresence == 0) nsnprintf( buf, sizeof(buf), "None" ); window_modifyText( uniedit_wid, "txtPresence", buf ); } else
/** * @brief Creates a new system. */ static void uniedit_newSys( double x, double y ) { char *name; StarSystem *sys; /* Get name. */ name = dialogue_inputRaw( "New Star System Creation", 1, 32, "What do you want to name the new system?" ); /* Abort. */ if (name == NULL) { dialogue_alert( "Star System creation aborted!" ); return; } /* Make sure there is no collision. */ if (uniedit_checkName( name )) { free(name); uniedit_newSys( x, y ); return; } /* Transform coordinates back to normal if zoomed */ x /= uniedit_zoom; y /= uniedit_zoom; /* Create the system. */ sys = system_new(); sys->name = name; sys->pos.x = x; sys->pos.y = y; sys->stars = STARS_DENSITY_DEFAULT; sys->radius = RADIUS_DEFAULT; /* Select new system. */ uniedit_deselect(); uniedit_selectAdd( sys ); if (conf.devautosave) dsys_saveSystem( sys ); }
/** * @brief System editor custom widget mouse handling. */ static void uniedit_mouse( unsigned int wid, SDL_Event* event, double mx, double my, double w, double h, void *data ) { (void) wid; (void) data; int i; double x,y, t; StarSystem *sys; SDLMod mod; t = 15.*15.; /* threshold */ /* Handle modifiers. */ mod = SDL_GetModState(); switch (event->type) { case SDL_MOUSEBUTTONDOWN: /* Must be in bounds. */ if ((mx < 0.) || (mx > w) || (my < 0.) || (my > h)) return; /* Zooming */ if (event->button.button == SDL_BUTTON_WHEELUP) uniedit_buttonZoom( 0, "btnZoomIn" ); else if (event->button.button == SDL_BUTTON_WHEELDOWN) uniedit_buttonZoom( 0, "btnZoomOut" ); /* selecting star system */ else { mx -= w/2 - uniedit_xpos; my -= h/2 - uniedit_ypos; if (uniedit_mode == UNIEDIT_NEWSYS) { uniedit_newSys( mx, my ); uniedit_mode = UNIEDIT_DEFAULT; return; } for (i=0; i<systems_nstack; i++) { sys = system_getIndex( i ); /* get position */ x = sys->pos.x * uniedit_zoom; y = sys->pos.y * uniedit_zoom; if ((pow2(mx-x)+pow2(my-y)) < t) { /* Try to find in selected systems - begin drag move. */ for (i=0; i<uniedit_nsys; i++) { /* Must match. */ if (uniedit_sys[i] != sys) continue; /* Detect double click to open system. */ if ((SDL_GetTicks() - uniedit_dragTime < UNIEDIT_DRAG_THRESHOLD*2) && (uniedit_moved < UNIEDIT_MOVE_THRESHOLD)) { if (uniedit_nsys == 1) { sysedit_open( uniedit_sys[0] ); return; } } /* Handle normal click. */ if (uniedit_mode == UNIEDIT_DEFAULT) { uniedit_dragSys = 1; uniedit_tsys = sys; /* Check modifier. */ if (mod & (KMOD_LCTRL | KMOD_RCTRL)) uniedit_tadd = 0; else uniedit_tadd = -1; uniedit_dragTime = SDL_GetTicks(); uniedit_moved = 0; } return; } if (uniedit_mode == UNIEDIT_DEFAULT) { /* Add the system if not selected. */ if (mod & (KMOD_LCTRL | KMOD_RCTRL)) uniedit_selectAdd( sys ); else { uniedit_deselect(); uniedit_selectAdd( sys ); } uniedit_tsys = NULL; /* Start dragging anyway. */ uniedit_dragSys = 1; uniedit_dragTime = SDL_GetTicks(); uniedit_moved = 0; } else if (uniedit_mode == UNIEDIT_JUMP) { uniedit_toggleJump( sys ); uniedit_mode = UNIEDIT_DEFAULT; } return; } } /* Start dragging. */ if ((uniedit_mode == UNIEDIT_DEFAULT) && !(mod & (KMOD_LCTRL | KMOD_RCTRL))) { uniedit_drag = 1; uniedit_dragTime = SDL_GetTicks(); uniedit_moved = 0; uniedit_tsys = NULL; } return; } break; case SDL_MOUSEBUTTONUP: if (uniedit_drag) { if ((SDL_GetTicks() - uniedit_dragTime < UNIEDIT_DRAG_THRESHOLD) && (uniedit_moved < UNIEDIT_MOVE_THRESHOLD)) { if (uniedit_tsys == NULL) uniedit_deselect(); else uniedit_selectAdd( uniedit_tsys ); } uniedit_drag = 0; } if (uniedit_dragSys) { if ((SDL_GetTicks() - uniedit_dragTime < UNIEDIT_DRAG_THRESHOLD) && (uniedit_moved < UNIEDIT_MOVE_THRESHOLD) && (uniedit_tsys != NULL)) { if (uniedit_tadd == 0) uniedit_selectRm( uniedit_tsys ); else { uniedit_deselect(); uniedit_selectAdd( uniedit_tsys ); } } uniedit_dragSys = 0; for (i=0; i<uniedit_nsys; i++) { dsys_saveSystem(uniedit_sys[i]); } } break; case SDL_MOUSEMOTION: /* Update mouse positions. */ uniedit_mx = mx; uniedit_my = my; /* Handle dragging. */ if (uniedit_drag) { /* axis is inverted */ uniedit_xpos -= event->motion.xrel; uniedit_ypos += event->motion.yrel; /* Update mouse movement. */ uniedit_moved += ABS( event->motion.xrel ) + ABS( event->motion.yrel ); } else if (uniedit_dragSys && (uniedit_nsys > 0)) { if ((uniedit_moved > UNIEDIT_MOVE_THRESHOLD) || (SDL_GetTicks() - uniedit_dragTime > UNIEDIT_DRAG_THRESHOLD)) { for (i=0; i<uniedit_nsys; i++) { uniedit_sys[i]->pos.x += ((double)event->motion.xrel) / uniedit_zoom; uniedit_sys[i]->pos.y -= ((double)event->motion.yrel) / uniedit_zoom; } } /* Update mouse movement. */ uniedit_moved += ABS( event->motion.xrel ) + ABS( event->motion.yrel ); } break; } }
/** * @brief Opens the system editor interface. */ void uniedit_open( unsigned int wid_unused, char *unused ) { (void) wid_unused; (void) unused; unsigned int wid; /* Pause. */ pause_game(); /* Needed to generate faction disk. */ map_setZoom( 1. ); /* Must have no diffs applied. */ diff_clear(); /* Reset some variables. */ uniedit_mode = UNIEDIT_DEFAULT; uniedit_drag = 0; uniedit_dragSys = 0; uniedit_tsys = NULL; uniedit_tadd = 0; uniedit_zoom = 1.; uniedit_xpos = 0.; uniedit_ypos = 0.; /* Create the window. */ wid = window_create( "Universe Editor", -1, -1, -1, -1 ); window_handleKeys( wid, uniedit_keys ); uniedit_wid = wid; /* Close button. */ window_addButton( wid, -20, 20, BUTTON_WIDTH, BUTTON_HEIGHT, "btnClose", "Close", uniedit_close ); /* Jump toggle. */ window_addButton( wid, -20, 20+(BUTTON_HEIGHT+20)*3, BUTTON_WIDTH, BUTTON_HEIGHT, "btnJump", "Jump", uniedit_btnJump ); /* Rename system. */ window_addButton( wid, -20, 20+(BUTTON_HEIGHT+20)*4, BUTTON_WIDTH, BUTTON_HEIGHT, "btnRename", "Rename", uniedit_btnRename ); /* Edit system. */ window_addButton( wid, -20, 20+(BUTTON_HEIGHT+20)*5, BUTTON_WIDTH, BUTTON_HEIGHT, "btnEdit", "Edit", uniedit_btnEdit ); /* New system. */ window_addButton( wid, -20, 20+(BUTTON_HEIGHT+20)*6, BUTTON_WIDTH, BUTTON_HEIGHT, "btnNew", "New Sys", uniedit_btnNew ); /* Open a system. */ window_addButton( wid, -20, 20+(BUTTON_HEIGHT+20)*7, BUTTON_WIDTH, BUTTON_HEIGHT, "btnOpen", "Open", uniedit_btnOpen ); /* Zoom buttons */ window_addButton( wid, 40, 20, 30, 30, "btnZoomIn", "+", uniedit_buttonZoom ); window_addButton( wid, 80, 20, 30, 30, "btnZoomOut", "-", uniedit_buttonZoom ); /* Presence. */ window_addText( wid, -20, -140, 90, 20, 0, "txtSPresence", &gl_smallFont, &cDConsole, "Presence:" ); window_addText( wid, -20, -140-gl_smallFont.h-5, 80, 100, 0, "txtPresence", &gl_smallFont, &cBlack, "N/A" ); /* Selected text. */ window_addText( wid, 140, 10, SCREEN_W - 80 - 30 - 30 - BUTTON_WIDTH - 20, 30, 0, "txtSelected", &gl_smallFont, &cBlack, NULL ); /* Actual viewport. */ window_addCust( wid, 20, -40, SCREEN_W - 150, SCREEN_H - 100, "cstSysEdit", 1, uniedit_render, uniedit_mouse, NULL ); window_custSetOverlay( wid, "cstSysEdit", uniedit_renderOverlay ); /* Deselect everything. */ uniedit_deselect(); }