From 6c7bb91cabd74d1f69c825c111ad6416879cb68f Mon Sep 17 00:00:00 2001 From: Zebbeni Date: Mon, 15 Sep 2014 00:03:32 -0600 Subject: [PATCH 1/3] first update to refactor menu panels --- button.pde => child_button.pde | 2 +- child_menuitem.pde | 37 ++++++++ element.pde | 7 +- global_functions.pde | 19 +++- global_variables.pde | 5 ++ panel_child.pde | 1 + parent_canvas.pde | 6 +- parent_interface.pde | 4 +- parent_scrollmenu.pde | 155 ++++++++++++--------------------- parent_toolbar.pde | 1 + template_handler.pde | 5 +- user_interface.pde | 11 ++- 12 files changed, 137 insertions(+), 116 deletions(-) rename button.pde => child_button.pde (92%) create mode 100644 child_menuitem.pde diff --git a/button.pde b/child_button.pde similarity index 92% rename from button.pde rename to child_button.pde index 2ba30fd..d676acc 100644 --- a/button.pde +++ b/child_button.pde @@ -12,7 +12,7 @@ public class Button extends ChildPanel public void clickThis() { - println("clicked ", name); +// println("clicked ", name); } public void updateThis() diff --git a/child_menuitem.pde b/child_menuitem.pde new file mode 100644 index 0000000..e2cd416 --- /dev/null +++ b/child_menuitem.pde @@ -0,0 +1,37 @@ +public class MenuItem extends ChildPanel +{ + Element element; + + MenuItem ( int xx, int yy, int ww , int hh , Element e) + { + x = xx; y = yy; wid = ww; hei = hh; element = e; + drawPG = createGraphics( wid, hei ); + } + + public void clickThis() + { + selectElement( element ); + } + + public void updateThis() + { + if(element.index == 2) + { + y = (element.index * hei) - scrollDist; + drawPG.fill( 200 ); +// if (element.selected) +// { +// drawPG.fill( 255 ); +// } +// else if(element.hovered) +// { +// drawPG.fill(215); +// } + drawPG.rect( x , y , wid , hei ); + +// drawPG.noStroke(); + drawPG.fill( 0 ); + drawPG.text( element.name, x + 25, y ); + } + } +} diff --git a/element.pde b/element.pde index fc2d50c..73dc5a3 100644 --- a/element.pde +++ b/element.pde @@ -2,6 +2,8 @@ public class Element { String name; int type; + int index; + int x; float realX; int y; @@ -10,19 +12,22 @@ public class Element float realWid; int hei; float realHei; + float hSquish = 1.0; String fontString = ""; //this is a String for the font file to load. PFont font; int fontSize = 0; String colorString = "FFFFFF"; color col = unhex("FFFFFFFF"); + boolean selected = false; boolean hovered = false; - Element (String nn, int tt, int xx, int yy, int ww, int hh) + Element (String nn, int tt, int idx, int xx, int yy, int ww, int hh) { name = nn; type = tt; + index = idx; x = xx; realX = x; y = yy; diff --git a/global_functions.pde b/global_functions.pde index b07755a..16ce150 100644 --- a/global_functions.pde +++ b/global_functions.pde @@ -16,25 +16,36 @@ void constrainOffsets() void addElement() { - Element e = new Element("New Element", IMG, 2, 2, 200, 250); + Element e = new Element("New Element", IMG, elements.size(), 2, 2, 200, 250); + elements.add(e); listbox.addItem(e); content.table.addColumn(e.name); } void removeElement() { - int selectedId = listbox.selectedItem; + int selectedId = selectedElement; if (selectedId != NONE) { - Element e = listbox.items.get(selectedId); + Element e = elements.get(selectedId); content.table.removeColumn(e.name); listbox.removeItem(e); } } +void selectElement(Element e) +{ + if( selectedElement != NONE) + { + elements.get(selectedElement).selected = false; + } + e.selected = true; + selectedElement = e.index; +} + public void handleArrowPress( ) { - int selectedId = listbox.selectedItem; + int selectedId = selectedElement; canvas.handleArrowPress( selectedId ); } diff --git a/global_variables.pde b/global_variables.pde index dda9f0b..78835f2 100644 --- a/global_variables.pde +++ b/global_variables.pde @@ -12,3 +12,8 @@ int move_step = 1; Object lastItemClicked; float zoom = 0.6; + +int scrollDist = 0; + +int selectedElement = NONE; +int hoverItem = NONE; diff --git a/panel_child.pde b/panel_child.pde index 7777b42..1bfdeeb 100644 --- a/panel_child.pde +++ b/panel_child.pde @@ -25,5 +25,6 @@ public abstract class ChildPanel extends Panel * draws thisPG to the parentPG */ parentPG.image( drawPG , x , y ); + println("x: ", x, "y: ", y); } } diff --git a/parent_canvas.pde b/parent_canvas.pde index a8c552f..249941c 100644 --- a/parent_canvas.pde +++ b/parent_canvas.pde @@ -4,7 +4,6 @@ public class TemplateCanvas extends ParentPanel PImage cardPic; PFont drawFont; - ArrayList elements; Table contents; int canvasWid; @@ -24,10 +23,9 @@ public class TemplateCanvas extends ParentPanel int lastMX; int lastMY; - TemplateCanvas (String nm, ArrayList e, Table conts, int xx, int yy, int canW, int canH) + TemplateCanvas (String nm, Table conts, int xx, int yy, int canW, int canH) { name = nm; x = xx; y = yy; wid = int(canW * zoom); hei = int(canH * zoom); - elements = e; canvasWid = canW; canvasHei = canH; contents = conts; @@ -265,7 +263,7 @@ public class TemplateCanvas extends ParentPanel } else { - println("Clicked ", name); +// println("Clicked ", name); } } diff --git a/parent_interface.pde b/parent_interface.pde index 40325a3..9d41c55 100644 --- a/parent_interface.pde +++ b/parent_interface.pde @@ -16,11 +16,13 @@ public class Interface extends ParentPanel void updateThis() { + wid = width; + hei = height; drawPG.background(100); } void clickThis() { - println("Clicked ", name); +// println("Clicked ", name); } } diff --git a/parent_scrollmenu.pde b/parent_scrollmenu.pde index 245c26e..9ca2536 100644 --- a/parent_scrollmenu.pde +++ b/parent_scrollmenu.pde @@ -2,10 +2,6 @@ public class Listbox extends ParentPanel { int listHeight; - ArrayList items; - int selectedItem = NONE; - int hoverItem = NONE; - int itemHeight; int itemWidth; @@ -13,13 +9,11 @@ public class Listbox extends ParentPanel int scrolltabHeight; int scrolltabX; int scrolltabY; - int scrollDist = 0; int maxScrollDist = 0; - int listStartAt = 0; - Listbox ( String nm , int xx, int yy, int ww, int hh, int ih, ArrayList e) + Listbox ( String nm , int xx, int yy, int ww, int hh, int ih ) { name = nm; x = xx; y = yy; wid = ww; hei = hh; wid = ww; @@ -28,7 +22,8 @@ public class Listbox extends ParentPanel itemHeight = ih; itemWidth = wid - scrolltabWidth; - items = e; + refreshItems(); + calculateListHeight(); updateScrollTab(); @@ -36,16 +31,25 @@ public class Listbox extends ParentPanel Interactive.add( this ); } - public void addItem ( Element item ) + public void refreshItems( ) { - if ( items == null ) items = new ArrayList(); - items.add( item ); + childPanels = new ArrayList(); + for( Element e : elements ) + { + addItem( e ); + } + } + + public void addItem ( Element e ) + { + MenuItem item = new MenuItem( 0 , 0 , itemWidth , itemHeight , e); + childPanels.add( item ); } public void removeItem ( Element item ) { - items.remove(item); - selectedItem = NONE; + elements.remove(item); + selectedElement = NONE; hoverItem = NONE; } @@ -57,43 +61,19 @@ public class Listbox extends ParentPanel } void updateThis () - { - updatePosition(); - drawPG.beginDraw(); - drawPG.background(200); - drawPG.noStroke(); - drawPG.fill( 100 ); - drawPG.rect( 0 , 0 - scrollDist, wid , listHeight ); - if ( items != null ) - { - for ( int i = 0; i < items.size(); i++ ) - { - drawPG.stroke( 80 ); - drawPG.fill( 200 ); - if (items.get(i).selected) - { - drawPG.fill( 255 ); - } - else if(items.get(i).hovered) - { - drawPG.fill(215); - } - drawPG.rect( 0, (i*itemHeight) - scrollDist, wid, itemHeight ); - - drawPG.noStroke(); - drawPG.fill( 0 ); - drawPG.text( items.get(i+listStartAt).name, 25, (i+1)*(itemHeight) - 20 - scrollDist ); - } - } - - //draw scrollbar - drawPG.stroke(80); - drawPG.fill(100); - drawPG.rect(itemWidth, 0, wid, hei); - drawPG.fill(200); - drawPG.rect(scrolltabX, scrolltabY, scrolltabWidth, scrolltabHeight); - - drawPG.endDraw(); + { + updatePosition(); + drawPG.background(200); + drawPG.noStroke(); + drawPG.fill( 100 ); + drawPG.rect( 0 , 0 - scrollDist, wid , listHeight ); + + //draw scrollbar + drawPG.stroke(80); + drawPG.fill(100); + drawPG.rect(itemWidth, 0, wid, hei); + drawPG.fill(200); + drawPG.rect(scrolltabX, scrolltabY, scrolltabWidth, scrolltabHeight); } /** @@ -104,7 +84,6 @@ public class Listbox extends ParentPanel hei = height - y - 100; updateMaxScrollDist(); updateScrollTab(); - drawPG = createGraphics(wid, hei); } void updateMaxScrollDist() @@ -114,7 +93,7 @@ public class Listbox extends ParentPanel } void calculateListHeight(){ - listHeight = itemHeight * items.size(); + listHeight = itemHeight * elements.size(); } void updateScrollTab() @@ -144,57 +123,34 @@ public class Listbox extends ParentPanel void hoverItem( int my ) { - int listClick = my + scrollDist - y; - int index = ceil(listClick / itemHeight); - if ( index >= items.size()) - { - if( hoverItem != NONE ) - { - items.get(hoverItem).hovered = false; - hoverItem = NONE; - } - } - else - { - if(hoverItem != NONE) - { - items.get(hoverItem).hovered = false; - } - hoverItem = index; - items.get(hoverItem).hovered = true; - } +// int listClick = my + scrollDist - y; +// int index = ceil(listClick / itemHeight); +// if ( index >= elements.size()) +// { +// if( hoverItem != NONE ) +// { +// elements.get(hoverItem).hovered = false; +// hoverItem = NONE; +// } +// } +// else +// { +// if(hoverItem != NONE) +// { +// elements.get(hoverItem).hovered = false; +// } +// hoverItem = index; +// elements.get(hoverItem).hovered = true; +// } } void unHover() { - if( hoverItem != NONE) - { - items.get(hoverItem).hovered = false; - hoverItem = NONE; - } - } - - void selectItem( int my ) - { - int listClick = my + scrollDist - y; - int index = int(listClick / itemHeight); - if(index >= items.size()) - { - if( selectedItem != NONE) - { - items.get(selectedItem).selected = false; - } - selectedItem = NONE; - } - else - { - if( selectedItem != NONE) - { - items.get(selectedItem).selected = false; - } - selectedItem = index; - items.get(selectedItem).selected = true; - } +// if( hoverItem != NONE) +// { +// elements.get(hoverItem).hovered = false; +// hoverItem = NONE; +// } } void clickScrollBar( int my ) @@ -204,7 +160,6 @@ public class Listbox extends ParentPanel void clickThis() { - println("Clicked ", name); } boolean inItem( int mx, int my) diff --git a/parent_toolbar.pde b/parent_toolbar.pde index ce7801a..50e1dca 100644 --- a/parent_toolbar.pde +++ b/parent_toolbar.pde @@ -26,6 +26,7 @@ public class Toolbar extends ParentPanel void updateThis() { + x = width - wid; drawPG.fill(200); drawPG.rect(0, 0, wid, hei); } diff --git a/template_handler.pde b/template_handler.pde index 55e880e..a76a936 100644 --- a/template_handler.pde +++ b/template_handler.pde @@ -75,9 +75,9 @@ public class TemplateHandler { tempTable = loadTable(filename, "header"); + int idx = 0; for (TableRow row : tempTable.rows()) { - String name = getElemName(row.getString("element")); int type = getType(row.getString("type")); int x = getX(row.getString("x")); @@ -85,7 +85,8 @@ public class TemplateHandler int w = getHei(row.getString("w")); int h = getWid(row.getString("h")); - Element e = new Element(name, type, x, y, w, h); + Element e = new Element(name, type, idx, x, y, w, h); + idx += 1; if (type == TXT) { diff --git a/user_interface.pde b/user_interface.pde index a32ac9a..280ebed 100644 --- a/user_interface.pde +++ b/user_interface.pde @@ -8,6 +8,8 @@ TemplateCanvas canvas; Toolbar tools; Interface mainInterface; +ArrayList elements; + void setup () { size(1000, 700); @@ -20,9 +22,12 @@ void setup () // create a list box template = new TemplateHandler(); - listbox = new Listbox("element menu", width - 150 , 35 , 150 , 400 , 50 , template.elements ); + + elements = template.elements; + + listbox = new Listbox("element menu", width - 150 , 35 , 150 , 400 , 50 ); content = new ContentHandler( ); - canvas = new TemplateCanvas("canvas", template.elements, content.table, 200, 30, 675, 1050); + canvas = new TemplateCanvas("canvas", content.table, 200, 30, 675, 1050); tools = new Toolbar("toolbar", width - 150 , 0 , 150 , 35 ); ArrayList parentPanels = new ArrayList(); @@ -53,7 +58,7 @@ void mouseMoved() void mouseDragged() { - if (canvas.handleDragged(mouseX,mouseY,listbox.selectedItem)){ + if (canvas.handleDragged(mouseX,mouseY,selectedElement)){ } } From e3459241889d6b8a704a328bec0608e5105f7d31 Mon Sep 17 00:00:00 2001 From: Zebbeni Date: Mon, 15 Sep 2014 22:02:16 -0600 Subject: [PATCH 2/3] fixed displaying problem --- child_menuitem.pde | 29 +++++++++++++---------------- panel_child.pde | 1 - 2 files changed, 13 insertions(+), 17 deletions(-) diff --git a/child_menuitem.pde b/child_menuitem.pde index e2cd416..85a3cf4 100644 --- a/child_menuitem.pde +++ b/child_menuitem.pde @@ -15,23 +15,20 @@ public class MenuItem extends ChildPanel public void updateThis() { - if(element.index == 2) + y = (element.index * hei) - scrollDist; + drawPG.fill( 200 ); + if (element.selected) { - y = (element.index * hei) - scrollDist; - drawPG.fill( 200 ); -// if (element.selected) -// { -// drawPG.fill( 255 ); -// } -// else if(element.hovered) -// { -// drawPG.fill(215); -// } - drawPG.rect( x , y , wid , hei ); - -// drawPG.noStroke(); - drawPG.fill( 0 ); - drawPG.text( element.name, x + 25, y ); + drawPG.fill( 255 ); } + else if(element.hovered) + { + drawPG.fill(215); + } + drawPG.rect( 0 , 0 , wid , hei ); + + drawPG.noStroke(); + drawPG.fill( 0 ); + drawPG.text( element.name, 25, 25 ); } } diff --git a/panel_child.pde b/panel_child.pde index 1bfdeeb..7777b42 100644 --- a/panel_child.pde +++ b/panel_child.pde @@ -25,6 +25,5 @@ public abstract class ChildPanel extends Panel * draws thisPG to the parentPG */ parentPG.image( drawPG , x , y ); - println("x: ", x, "y: ", y); } } From 1624e6dbfc5b0ea1c30254fcdb97a83276a4e243 Mon Sep 17 00:00:00 2001 From: Zebbeni Date: Mon, 15 Sep 2014 23:17:26 -0600 Subject: [PATCH 3/3] added hover --- child_button.pde | 7 +++- child_menuitem.pde | 7 +++- global_functions.pde | 21 ++++++++++ global_variables.pde | 2 +- panel.pde | 5 ++- panel_child.pde | 14 +++++++ panel_parent.pde | 23 ++++++++++- parent_canvas.pde | 5 +++ parent_interface.pde | 7 +++- parent_scrollmenu.pde | 90 +++++++++++++++++++++++-------------------- parent_toolbar.pde | 5 +++ user_interface.pde | 4 +- 12 files changed, 139 insertions(+), 51 deletions(-) diff --git a/child_button.pde b/child_button.pde index d676acc..35aafe4 100644 --- a/child_button.pde +++ b/child_button.pde @@ -12,7 +12,12 @@ public class Button extends ChildPanel public void clickThis() { -// println("clicked ", name); + println("clicked on ", name); + } + + public void hoverThis() + { + println("hovering on ", name); } public void updateThis() diff --git a/child_menuitem.pde b/child_menuitem.pde index 85a3cf4..51624de 100644 --- a/child_menuitem.pde +++ b/child_menuitem.pde @@ -13,10 +13,15 @@ public class MenuItem extends ChildPanel selectElement( element ); } + public void hoverThis() + { + hoverElement(element); + } + public void updateThis() { y = (element.index * hei) - scrollDist; - drawPG.fill( 200 ); + drawPG.fill( 210 ); if (element.selected) { drawPG.fill( 255 ); diff --git a/global_functions.pde b/global_functions.pde index 16ce150..e711516 100644 --- a/global_functions.pde +++ b/global_functions.pde @@ -43,6 +43,27 @@ void selectElement(Element e) selectedElement = e.index; } +void hoverElement(Element e) +{ + if( e == null ) + { + if( hoveredElement != NONE) + { + elements.get(hoveredElement).hovered = false; + } + hoveredElement = NONE; + } + else + { + if( hoveredElement != NONE) + { + elements.get(hoveredElement).hovered = false; + } + e.hovered = true; + hoveredElement = e.index; + } +} + public void handleArrowPress( ) { int selectedId = selectedElement; diff --git a/global_variables.pde b/global_variables.pde index 78835f2..79ba2c6 100644 --- a/global_variables.pde +++ b/global_variables.pde @@ -16,4 +16,4 @@ float zoom = 0.6; int scrollDist = 0; int selectedElement = NONE; -int hoverItem = NONE; +int hoveredElement = NONE; diff --git a/panel.pde b/panel.pde index fd4b406..84726f2 100644 --- a/panel.pde +++ b/panel.pde @@ -10,6 +10,9 @@ public abstract class Panel public abstract void clickThis(); public abstract boolean click( int mx , int my ); + public abstract void hoverThis(); + public abstract boolean hover( int mx , int my); + public abstract void updateThis(); public abstract void updateDraw(); public abstract void drawToBuffer( PGraphics parentPG); @@ -27,7 +30,7 @@ public abstract class Panel /** * Returns true if the given mouse X and mouse Y lie in this panel */ - boolean isHere = (mx > x && mx < (x + wid) && my > y && y < (my + hei)); + boolean isHere = (mx > x && mx < (x + wid) && my > y && my < (y + hei)); return isHere; } } diff --git a/panel_child.pde b/panel_child.pde index 7777b42..d506a6a 100644 --- a/panel_child.pde +++ b/panel_child.pde @@ -14,6 +14,20 @@ public abstract class ChildPanel extends Panel return clickedInHere; } + public boolean hover( int mx , int my ) + { + /** + * Returns true if the click occurred in this panel + * calls class-specific click function with mouse X and mouse Y + */ + boolean hoveredInHere = isInPanel( mx , my ); + if ( hoveredInHere ) + { + hoverThis(); + } + return hoveredInHere; + } + public void updateDraw() { updateThis(); diff --git a/panel_parent.pde b/panel_parent.pde index ed85590..1cd5be9 100644 --- a/panel_parent.pde +++ b/panel_parent.pde @@ -22,10 +22,31 @@ public abstract class ParentPanel extends Panel clickThis(); } } - return clickedInHere; } + public boolean hover( int mx , int my) + { + /* + * Passes relative mouse position to all children until it finds the one hovered + */ + boolean hoveredInHere = isInPanel( mx , my ); + + if( hoveredInHere ) + { + boolean hoveredChild = false; + for( int i = 0; i < childPanels.size() && !hoveredChild ; i++ ) + { + hoveredChild = childPanels.get(i).click( mx - x, my - y); + } + if( !hoveredChild ) + { + hoverThis(); + } + } + return hoveredInHere; + } + public void updateDraw() { updateThis(); diff --git a/parent_canvas.pde b/parent_canvas.pde index 249941c..deeffdf 100644 --- a/parent_canvas.pde +++ b/parent_canvas.pde @@ -253,6 +253,11 @@ public class TemplateCanvas extends ParentPanel lastMY = my; } + void hoverThis() + { + println("Hovering on ", name); + } + void clickThis() { if( isDragging ) diff --git a/parent_interface.pde b/parent_interface.pde index 9d41c55..d6aab5d 100644 --- a/parent_interface.pde +++ b/parent_interface.pde @@ -23,6 +23,11 @@ public class Interface extends ParentPanel void clickThis() { -// println("Clicked ", name); + println("Clicked ", name); + } + + void hoverThis() + { + println("Hovering on ", name); } } diff --git a/parent_scrollmenu.pde b/parent_scrollmenu.pde index 9ca2536..16a32e9 100644 --- a/parent_scrollmenu.pde +++ b/parent_scrollmenu.pde @@ -50,7 +50,7 @@ public class Listbox extends ParentPanel { elements.remove(item); selectedElement = NONE; - hoverItem = NONE; + hoveredElement = NONE; } // called from manager @@ -104,54 +104,60 @@ public class Listbox extends ParentPanel scrolltabY = int(float(hei * scrollDist) / float(listHeight)); //tabY is relative to drawPG } - boolean handleMoved ( int mx, int my) - { - if(isInPanel(mx,my)) - { - if(inItem(mx,my)) - { - hoverItem(my); - } - return true; - } - else - { - unHover(); - return false; - } - } - - void hoverItem( int my ) - { -// int listClick = my + scrollDist - y; -// int index = ceil(listClick / itemHeight); -// if ( index >= elements.size()) +// boolean handleMoved ( int mx, int my) +// { +// if(isInPanel(mx,my)) // { -// if( hoverItem != NONE ) +// if(inItem(mx,my)) // { -// elements.get(hoverItem).hovered = false; -// hoverItem = NONE; -// } -// } +// hoverItem(my); +// } +// return true; +// } // else // { -// if(hoverItem != NONE) -// { -// elements.get(hoverItem).hovered = false; -// } -// hoverItem = index; -// elements.get(hoverItem).hovered = true; +// unHover(); +// return false; // } - } +// } - void unHover() + void hoverThis() { -// if( hoverItem != NONE) -// { -// elements.get(hoverItem).hovered = false; -// hoverItem = NONE; -// } - } + hoverElement( null ); + println("Hovering on ", name); + } + +// void hoverItem( int my ) +// { +//// int listClick = my + scrollDist - y; +//// int index = ceil(listClick / itemHeight); +//// if ( index >= elements.size()) +//// { +//// if( hoverItem != NONE ) +//// { +//// elements.get(hoverItem).hovered = false; +//// hoverItem = NONE; +//// } +//// } +//// else +//// { +//// if(hoverItem != NONE) +//// { +//// elements.get(hoverItem).hovered = false; +//// } +//// hoverItem = index; +//// elements.get(hoverItem).hovered = true; +//// } +// } +// +// void unHover() +// { +//// if( hoverItem != NONE) +//// { +//// elements.get(hoverItem).hovered = false; +//// hoverItem = NONE; +//// } +// } void clickScrollBar( int my ) { diff --git a/parent_toolbar.pde b/parent_toolbar.pde index 50e1dca..2a184b3 100644 --- a/parent_toolbar.pde +++ b/parent_toolbar.pde @@ -35,4 +35,9 @@ public class Toolbar extends ParentPanel { println("Clicked ", name); } + + void hoverThis() + { + println("Hovering on ", name); + } } diff --git a/user_interface.pde b/user_interface.pde index 280ebed..ffb5e38 100644 --- a/user_interface.pde +++ b/user_interface.pde @@ -51,9 +51,7 @@ void draw () void mouseMoved() { - if (listbox.handleMoved(mouseX,mouseY)){ - - } + mainInterface.hover( mouseX , mouseY ); } void mouseDragged()