|
| 1 | +public void itemClicked ( int i, Object item ) |
| 2 | +{ |
| 3 | + lastItemClicked = item; |
| 4 | +} |
| 5 | + |
| 6 | +public class Listbox |
| 7 | +{ |
| 8 | + PGraphics listPG; |
| 9 | + int x, y, listHeight; |
| 10 | + |
| 11 | + ArrayList<Element> items; |
| 12 | + int selectedItem = NONE; |
| 13 | + int hoverItem = NONE; |
| 14 | + |
| 15 | + int itemHeight; |
| 16 | + int itemWidth; |
| 17 | + |
| 18 | + int scrolltabWidth = 15; |
| 19 | + int scrolltabHeight; |
| 20 | + int scrolltabX; |
| 21 | + int scrolltabY; |
| 22 | + int scrollDist = 0; |
| 23 | + |
| 24 | + int maxScrollDist = 0; |
| 25 | + |
| 26 | + int pgHeight; |
| 27 | + int pgWidth; |
| 28 | + int listStartAt = 0; |
| 29 | + |
| 30 | + Listbox ( int xx, int yy, int ww, int hh, int ih, ArrayList<Element> e ) |
| 31 | + { |
| 32 | + x = xx; y = yy; |
| 33 | + pgWidth = ww; |
| 34 | + pgHeight = hh; |
| 35 | + listPG = createGraphics(pgWidth, pgHeight); |
| 36 | + |
| 37 | + itemHeight = ih; itemWidth = pgWidth - scrolltabWidth; |
| 38 | + |
| 39 | + items = e; |
| 40 | + calculateListHeight(); |
| 41 | + updateScrollTab(); |
| 42 | + |
| 43 | + // register it |
| 44 | + Interactive.add( this ); |
| 45 | + } |
| 46 | + |
| 47 | + public void addItem ( Element item ) |
| 48 | + { |
| 49 | + if ( items == null ) items = new ArrayList(); |
| 50 | + items.add( item ); |
| 51 | + } |
| 52 | + |
| 53 | + public void removeItem ( Element item ) |
| 54 | + { |
| 55 | + items.remove(item); |
| 56 | + selectedItem = NONE; |
| 57 | + hoverItem = NONE; |
| 58 | + } |
| 59 | + |
| 60 | + // called from manager |
| 61 | + void mouseScrolled ( float step ) |
| 62 | + { |
| 63 | + scrollDist += (step*5); |
| 64 | + scrollDist = constrain( scrollDist, 0, maxScrollDist <= 0 ? 0 : maxScrollDist ); |
| 65 | + } |
| 66 | + |
| 67 | + void drawList () |
| 68 | + { |
| 69 | + updatePosition(); |
| 70 | + listPG.beginDraw(); |
| 71 | + listPG.background(200); |
| 72 | + listPG.noStroke(); |
| 73 | + listPG.fill( 100 ); |
| 74 | + listPG.rect( 0 , 0 - scrollDist, pgWidth , listHeight ); |
| 75 | + if ( items != null ) |
| 76 | + { |
| 77 | + for ( int i = 0; i < items.size(); i++ ) |
| 78 | + { |
| 79 | + listPG.stroke( 80 ); |
| 80 | + listPG.fill( 200 ); |
| 81 | + if (items.get(i).selected) |
| 82 | + { |
| 83 | + listPG.fill( 255 ); |
| 84 | + } |
| 85 | + else if(items.get(i).hovered) |
| 86 | + { |
| 87 | + listPG.fill(215); |
| 88 | + } |
| 89 | + listPG.rect( 0, (i*itemHeight) - scrollDist, pgWidth, itemHeight ); |
| 90 | + |
| 91 | + listPG.noStroke(); |
| 92 | + listPG.fill( 0 ); |
| 93 | + listPG.text( items.get(i+listStartAt).name, 25, (i+1)*(itemHeight) - 20 - scrollDist ); |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + //draw scrollbar |
| 98 | + listPG.stroke(80); |
| 99 | + listPG.fill(100); |
| 100 | + listPG.rect(itemWidth, 0, pgWidth, pgHeight); |
| 101 | + listPG.fill(200); |
| 102 | + listPG.rect(scrolltabX, scrolltabY, scrolltabWidth, scrolltabHeight); |
| 103 | + |
| 104 | + listPG.endDraw(); |
| 105 | + } |
| 106 | + |
| 107 | + /** |
| 108 | + * update x position and list height in case window is resized |
| 109 | + */ |
| 110 | + void updatePosition(){ |
| 111 | + x = width - pgWidth; |
| 112 | + pgHeight = height - y - 100; |
| 113 | + updateMaxScrollDist(); |
| 114 | + updateScrollTab(); |
| 115 | + listPG = createGraphics(pgWidth, pgHeight); |
| 116 | + } |
| 117 | + |
| 118 | + void updateMaxScrollDist() |
| 119 | + { |
| 120 | + calculateListHeight(); |
| 121 | + maxScrollDist = listHeight - pgHeight; |
| 122 | + } |
| 123 | + |
| 124 | + void calculateListHeight(){ |
| 125 | + listHeight = itemHeight * items.size(); |
| 126 | + } |
| 127 | + |
| 128 | + void updateScrollTab() |
| 129 | + { |
| 130 | + scrolltabHeight = int(float(pgHeight * pgHeight) / float(listHeight)); |
| 131 | + scrolltabHeight = constrain(scrolltabHeight, 0, pgHeight); |
| 132 | + scrolltabX = itemWidth; |
| 133 | + scrolltabY = int(float(pgHeight * scrollDist) / float(listHeight)); //tabY is relative to listPG |
| 134 | + } |
| 135 | + |
| 136 | + boolean handleMoved ( int mx, int my) |
| 137 | + { |
| 138 | + if(inArea(mx,my)) |
| 139 | + { |
| 140 | + if(inItem(mx,my)) |
| 141 | + { |
| 142 | + hoverItem(my); |
| 143 | + } |
| 144 | + return true; |
| 145 | + } |
| 146 | + else |
| 147 | + { |
| 148 | + unHover(); |
| 149 | + return false; |
| 150 | + } |
| 151 | + } |
| 152 | + |
| 153 | + boolean handleClicked ( int mx, int my) |
| 154 | + { |
| 155 | + if(inArea(mx,my)) |
| 156 | + { |
| 157 | + if(inItem(mx,my)) |
| 158 | + { |
| 159 | + selectItem(my); |
| 160 | + } |
| 161 | + else if(inScrollbar(mx,my)) |
| 162 | + { |
| 163 | + clickScrollBar(my); |
| 164 | + } |
| 165 | + return true; |
| 166 | + } |
| 167 | + else |
| 168 | + { |
| 169 | + return false; |
| 170 | + } |
| 171 | + } |
| 172 | + |
| 173 | + void hoverItem( int my ) |
| 174 | + { |
| 175 | + int listClick = my + scrollDist - y; |
| 176 | + int index = ceil(listClick / itemHeight); |
| 177 | + if ( index >= items.size()) |
| 178 | + { |
| 179 | + if( hoverItem != NONE ) |
| 180 | + { |
| 181 | + items.get(hoverItem).hovered = false; |
| 182 | + hoverItem = NONE; |
| 183 | + } |
| 184 | + } |
| 185 | + else |
| 186 | + { |
| 187 | + if(hoverItem != NONE) |
| 188 | + { |
| 189 | + items.get(hoverItem).hovered = false; |
| 190 | + } |
| 191 | + hoverItem = index; |
| 192 | + items.get(hoverItem).hovered = true; |
| 193 | + } |
| 194 | + } |
| 195 | + |
| 196 | + void unHover() |
| 197 | + { |
| 198 | + if( hoverItem != NONE) |
| 199 | + { |
| 200 | + items.get(hoverItem).hovered = false; |
| 201 | + hoverItem = NONE; |
| 202 | + } |
| 203 | + } |
| 204 | + |
| 205 | + void selectItem( int my ) |
| 206 | + { |
| 207 | + int listClick = my + scrollDist - y; |
| 208 | + int index = int(listClick / itemHeight); |
| 209 | + if(index >= items.size()) |
| 210 | + { |
| 211 | + if( selectedItem != NONE) |
| 212 | + { |
| 213 | + items.get(selectedItem).selected = false; |
| 214 | + } |
| 215 | + selectedItem = NONE; |
| 216 | + } |
| 217 | + else |
| 218 | + { |
| 219 | + if( selectedItem != NONE) |
| 220 | + { |
| 221 | + items.get(selectedItem).selected = false; |
| 222 | + } |
| 223 | + selectedItem = index; |
| 224 | + items.get(selectedItem).selected = true; |
| 225 | + } |
| 226 | + } |
| 227 | + |
| 228 | + void clickScrollBar( int my ) |
| 229 | + { |
| 230 | + println("clicked the scrollbar"); |
| 231 | + } |
| 232 | + |
| 233 | + boolean inArea( int mx, int my) |
| 234 | + { |
| 235 | + boolean isInArea = (mx > x && mx < x + pgWidth && my > y && my < y + pgHeight); |
| 236 | + return isInArea; |
| 237 | + } |
| 238 | + |
| 239 | + boolean inItem( int mx, int my) |
| 240 | + { |
| 241 | + boolean isInItem = (mx < x + itemWidth); |
| 242 | + return isInItem; |
| 243 | + } |
| 244 | + |
| 245 | + boolean inScrollbar( int mx, int my) |
| 246 | + { |
| 247 | + boolean isInArea = (mx >= x + itemWidth); |
| 248 | + return isInArea; |
| 249 | + } |
| 250 | +} |
| 251 | + |
0 commit comments