Skip to content

Commit

Permalink
Select pieces from the bar and add them to the level
Browse files Browse the repository at this point in the history
  • Loading branch information
kieranmillar committed Nov 20, 2017
1 parent 7bf4af4 commit 8ab1c23
Show file tree
Hide file tree
Showing 8 changed files with 91 additions and 11 deletions.
11 changes: 11 additions & 0 deletions src/Editor/bar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,17 @@ void Bar::changeType(int t)
resizeBarScrollRect(window_ptr->width, window_ptr->height);
}

int Bar::getPieceIDByScreenPos(int mousePos)
{
int piece = mousePos + barScrollX - BAR_HEIGHT;
piece /= PIECESIZE;

if (piece > style_ptr->object[type].size())
return -1;
int id = style_ptr->object[type][piece].id;
return id;
}

void Bar::draw( void )
{
{ // bar background in the absense of a proper graphic
Expand Down
2 changes: 2 additions & 0 deletions src/Editor/bar.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ class Bar

void changeType( int t);

int getPieceIDByScreenPos ( int mousePos );

void draw( void );

Bar(void) { /* nothing to do */ };
Expand Down
25 changes: 22 additions & 3 deletions src/Editor/canvas.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
*/

/*
This file includes code to manage drawing in the editor, and the properties fo the part of the screen
This file includes code to manage drawing in the editor, and the properties of the part of the screen
where the level is displayed
*/

Expand Down Expand Up @@ -114,7 +114,7 @@ void Canvas::draw()

SDL_SetRenderTarget(window_ptr->screen_renderer, NULL);
SDL_RenderCopy(window_ptr->screen_renderer, window_ptr->screen_texture, NULL, NULL);
SDL_RenderPresent(window_ptr->screen_renderer);



}
Expand All @@ -134,6 +134,25 @@ void Canvas::draw_selection_box(int x, int y, int width, int height)
SDL_RenderDrawRect(window_ptr->screen_renderer, &r);
}

void Canvas::drawHeldObject(int holdingType, int holdingID, int x, int y)
{
int scrollOffsetX, scrollOffsetY, drawX, drawY;
if (y < height)
{
scrollOffsetX = (scroll_x % 8) * zoom;
scrollOffsetY = (scroll_y % 2) * zoom;
drawX = x - ((x + scrollOffsetX) % (8 * zoom));
drawY = y - ((y + scrollOffsetY) % (2 * zoom));
}
else
{
drawX = x;
drawY = y;
}
int drawID = style_ptr->object_by_id(holdingType, holdingID);
style_ptr->draw_object_texture(window_ptr, drawX, drawY, holdingType, drawID, zoom, NULL);
}

void Canvas::draw_dashed_level_border(borderType type, int pos, int offset)
{
//We have an offset so the lines don't scroll out of synch with the view when scrolling
Expand All @@ -155,7 +174,7 @@ void Canvas::draw_dashed_level_border(borderType type, int pos, int offset)
}
if (type == vertical)
{
end = window_ptr->height;
end = height;
x1 = pos;
x2 = pos;
y1 = 0 - initialOffset;
Expand Down
2 changes: 2 additions & 0 deletions src/Editor/canvas.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ class Canvas

void draw_selection_box(int x, int y, int width, int height);

void drawHeldObject(int holdingType, int holdingID, int x, int y);

enum borderType { horizontal, vertical };
void draw_dashed_level_border(borderType type, int pos, int offset);

Expand Down
14 changes: 13 additions & 1 deletion src/Editor/editor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ bool Editor::paste( void )
selection.clear(); // maybe delete selection instead?

for (Clipboard::const_iterator i = clipboard.begin(); i != clipboard.end(); ++i)
{
{
const Level::Object::Index &index = i->first;

level.object[index.type].push_back(i->second);
Expand All @@ -141,6 +141,18 @@ bool Editor::paste( void )
return canvas.redraw = !clipboard.empty();
}

bool Editor::addObject(int idToAdd, int typeToAdd, int xToAdd, int yToAdd)
{
Level::Object o;

o.id = idToAdd;
o.x = xToAdd;
o.y = yToAdd;
level.object[typeToAdd].push_back(o);

return canvas.redraw = true;
}

bool Editor::decrease_obj_id( void )
{
for (Selection::const_iterator i = selection.begin(); i != selection.end(); ++i)
Expand Down
2 changes: 2 additions & 0 deletions src/Editor/editor.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ class Editor
bool select_none( void );
bool select_all( void );

bool addObject(int idToAdd, int typeToAdd, int xToAdd, int yToAdd);

bool copy_selected( void );
bool paste( void );
bool decrease_obj_id(void);
Expand Down
44 changes: 37 additions & 7 deletions src/Editor/input.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ void Editor_input::load(void)
dragging = false;
leftScrollButtonHolding = false;
rightScrollButtonHolding = false;
holdingID = -1;
holdingType = -1;
}

void Editor_input::handleEvents(SDL_Event event)
Expand Down Expand Up @@ -99,24 +101,48 @@ void Editor_input::handleEvents(SDL_Event event)
{
canvas_ptr->mouse_remainder_x = 0;
canvas_ptr->mouse_remainder_y = 0;
}

if (e.button == SDL_BUTTON_LEFT || e.button == SDL_BUTTON_RIGHT)
{
if (mouse_y_window < canvas_ptr->height)
// canvas
{
editor_ptr->select(mouse_x, mouse_y, ctrl_down);
if (holdingType != -1 && holdingID != -1)
{
editor_ptr->addObject(holdingID, holdingType, mouse_x - (mouse_x % 8), mouse_y - (mouse_y % 2));

Level::Object::Index o(holdingType, level_ptr->object[holdingType].size() - 1);
editor_ptr->selection.insert(o);

holdingType = -1;
holdingID = -1;
}
else
{
editor_ptr->select(mouse_x, mouse_y, ctrl_down);
}
}
else if (mouse_x_window < BAR_HEIGHT)
// options panel
{

holdingType = -1;
holdingID = -1;
}
else if (mouse_y_window < window_ptr->height - 16)
// piece browser
{

if (holdingType == -1 && holdingID == -1)
{
int pieceSelected = bar_ptr->getPieceIDByScreenPos(mouse_x_window);
if (pieceSelected != -1)
{
holdingType = bar_ptr->type;
holdingID = pieceSelected;
editor_ptr->select_none();
}
}
else
{
holdingType = -1;
holdingID = -1;
}
}
else if (mouse_x_window < BAR_HEIGHT + 16)
// piece browser scroll bar left button
Expand Down Expand Up @@ -289,6 +315,10 @@ void Editor_input::handleEvents(SDL_Event event)
}

canvas_ptr->draw();
if (holdingType != -1 && holdingID != -1)
canvas_ptr->drawHeldObject(holdingType, holdingID, mouse_x_window, mouse_y_window);

SDL_RenderPresent(window_ptr->screen_renderer);

break;
}
Expand Down
2 changes: 2 additions & 0 deletions src/Editor/input.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ class Editor_input
Sint32 mouse_prev_x, mouse_prev_y;
bool dragging, leftScrollButtonHolding, rightScrollButtonHolding;

int holdingID, holdingType;

void setReferences(Window * w, Editor * e, Bar * b, Canvas * c, Style * s, Level * l);
void load(void);

Expand Down

0 comments on commit 8ab1c23

Please sign in to comment.