[207] | 1 | // Copyright (C) 2003 Brian J. Swetland
|
---|
| 2 | // See LICENSE for redistribution terms
|
---|
| 3 |
|
---|
| 4 | package net.frotz.rpn;
|
---|
| 5 |
|
---|
| 6 | import dgs.libs.hipfloat.hipfloat;
|
---|
| 7 | import dgs.libs.hipfloat.hipfloatError;
|
---|
| 8 |
|
---|
| 9 | import danger.app.Application;
|
---|
| 10 | import danger.app.Event;
|
---|
| 11 | import danger.app.ResourceDatabase;
|
---|
| 12 | import danger.ui.Font;
|
---|
| 13 | import danger.ui.Pen;
|
---|
| 14 | import danger.ui.Color;
|
---|
| 15 | import danger.ui.StaticTextBox;
|
---|
| 16 | import danger.ui.ScreenWindow;
|
---|
| 17 |
|
---|
| 18 | public class CalcWindow extends ScreenWindow
|
---|
| 19 | implements ResourceEvents, Resources
|
---|
| 20 | {
|
---|
| 21 | public CalcWindow(RPN rpn) {
|
---|
| 22 | super("RPN Calculator");
|
---|
| 23 |
|
---|
| 24 | setIcon(rpn.getBundle().getSmallIcon());
|
---|
| 25 | rdb = rpn.getResources();
|
---|
| 26 | rdb.addToMenuFromResource(getActionMenu(),MENU_MAIN,this,null);
|
---|
| 27 |
|
---|
| 28 | font = Font.findFont("BortBold12");
|
---|
| 29 | height = font.getAscent() + font.getDescent() + 2;
|
---|
| 30 |
|
---|
| 31 | help = new StaticTextBox();
|
---|
| 32 | help.setPosition(getWidth()/2 - 2, 0);
|
---|
| 33 | help.setSize(getWidth()/2 + 2, getHeight() - height);
|
---|
| 34 | help.setFont(Font.findFont("Knob7"));
|
---|
| 35 | help.setText(HELP);
|
---|
| 36 | help.setTitle("Quick Reference");
|
---|
| 37 | help.show();
|
---|
| 38 | addChild(help);
|
---|
| 39 | }
|
---|
| 40 |
|
---|
| 41 | public boolean receiveEvent(Event e){
|
---|
| 42 | switch(e.type){
|
---|
| 43 | case DO_TOGGLE_HELP:
|
---|
| 44 | show_help = !show_help;
|
---|
| 45 | invalidate();
|
---|
| 46 | return true;
|
---|
| 47 | case DO_TOGGLE_SCI:
|
---|
| 48 | sci_mode = !sci_mode;
|
---|
| 49 | invalidate();
|
---|
| 50 | return true;
|
---|
| 51 | case DO_ABOUT_BOX:
|
---|
| 52 | (rdb.getDialog(DIALOG_ABOUT)).show();
|
---|
| 53 | return true;
|
---|
| 54 | default:
|
---|
| 55 | return super.receiveEvent(e);
|
---|
| 56 | }
|
---|
| 57 | }
|
---|
| 58 |
|
---|
| 59 | public boolean eventWidgetUp(int inWhichWidget, Event inEvent) {
|
---|
| 60 | switch (inWhichWidget) {
|
---|
| 61 | case Event.DEVICE_BUTTON_CANCEL:
|
---|
| 62 | Application.getCurrentApp().returnToLauncher();
|
---|
| 63 | return true;
|
---|
| 64 |
|
---|
| 65 | case Event.DEVICE_BUTTON_BACK:
|
---|
| 66 | Application.getCurrentApp().returnToLauncher();
|
---|
| 67 | return true;
|
---|
| 68 | }
|
---|
| 69 | return super.eventWidgetUp(inWhichWidget, inEvent);
|
---|
| 70 | }
|
---|
| 71 |
|
---|
| 72 | public void paint(Pen p) {
|
---|
| 73 | clear(p);
|
---|
| 74 |
|
---|
| 75 | int y = getHeight() - 3;
|
---|
| 76 | int ptr = SP;
|
---|
| 77 |
|
---|
| 78 | p.setFont(font);
|
---|
| 79 |
|
---|
| 80 | p.setColor(Color.BLACK);
|
---|
| 81 | p.fillRect(0, y - height + 3, getWidth(), getHeight());
|
---|
| 82 | p.setColor(Color.WHITE);
|
---|
| 83 | if(EP > 0){
|
---|
| 84 | p.drawText(3, y, ENTRY, 0, EP);
|
---|
| 85 | }
|
---|
| 86 | p.setColor(Color.BLACK);
|
---|
| 87 | y -= height;
|
---|
| 88 |
|
---|
| 89 | while((y >= 5) && (ptr > 0)){
|
---|
| 90 | ptr--;
|
---|
| 91 | int count = STACK[ptr].toCharArray(scratch, sci_mode);
|
---|
| 92 | p.drawText(3, y, scratch, 0, count);
|
---|
| 93 | y -= height;
|
---|
| 94 | }
|
---|
| 95 |
|
---|
| 96 | if(show_help) {
|
---|
| 97 | paintChildren(p);
|
---|
| 98 | }
|
---|
| 99 | }
|
---|
| 100 |
|
---|
| 101 |
|
---|
| 102 | public boolean eventKeyDown(char c, Event event) {
|
---|
| 103 | switch(c) {
|
---|
| 104 | case 8:
|
---|
| 105 | if(EP > 0){
|
---|
| 106 | /* kinda gross, but ensures we end up in
|
---|
| 107 | the right input state, just as if the
|
---|
| 108 | user had typed it to this point */
|
---|
| 109 | int ptr;
|
---|
| 110 | int max = EP - 1;
|
---|
| 111 | EP = 0;
|
---|
| 112 | dot = 0;
|
---|
| 113 | ee = 0;
|
---|
| 114 | for(ptr = 0; ptr < max; ptr++){
|
---|
| 115 | eventKeyDown(ENTRY[ptr], event);
|
---|
| 116 | }
|
---|
| 117 | } else {
|
---|
| 118 | if(SP > 0){
|
---|
| 119 | pop();
|
---|
| 120 | }
|
---|
| 121 | }
|
---|
| 122 | break;
|
---|
| 123 |
|
---|
| 124 | case '0': case '1': case '2': case '3': case '4':
|
---|
| 125 | case '5': case '6': case '7': case '8': case '9':
|
---|
| 126 | ENTRY[EP++] = c;
|
---|
| 127 | break;
|
---|
| 128 | case '.':
|
---|
| 129 | if((dot == 0) && (ee == 0)){
|
---|
| 130 | dot = 1;
|
---|
| 131 | ENTRY[EP++] = '.';
|
---|
| 132 | }
|
---|
| 133 | break;
|
---|
| 134 | case 'E':
|
---|
| 135 | if((EP > 0) && (ee == 0)){
|
---|
| 136 | ee = 1;
|
---|
| 137 | ENTRY[EP++] = 'E';
|
---|
| 138 | }
|
---|
| 139 | break;
|
---|
| 140 | case '-':
|
---|
| 141 | if((EP > 0) && (ee == 1)){
|
---|
| 142 | ee = 2;
|
---|
| 143 | ENTRY[EP++] = '-';
|
---|
| 144 | break;
|
---|
| 145 | }
|
---|
| 146 | /* fall through */
|
---|
| 147 | case 's':
|
---|
| 148 | accept();
|
---|
| 149 | if(SP > 1){
|
---|
| 150 | hipfloat b = pop();
|
---|
| 151 | hipfloat a = pop();
|
---|
| 152 | push(a.sub(b));
|
---|
| 153 | }
|
---|
| 154 | break;
|
---|
| 155 | case '^':
|
---|
| 156 | case 'p':
|
---|
| 157 | accept();
|
---|
| 158 | if(SP > 1){
|
---|
| 159 | hipfloat b = pop();
|
---|
| 160 | hipfloat a = pop();
|
---|
| 161 | push(a.pow(b));
|
---|
| 162 | }
|
---|
| 163 | break;
|
---|
| 164 | case '+':
|
---|
| 165 | case 'a':
|
---|
| 166 | accept();
|
---|
| 167 | if(SP > 1){
|
---|
| 168 | hipfloat b = pop();
|
---|
| 169 | hipfloat a = pop();
|
---|
| 170 | push(a.add(b));
|
---|
| 171 | }
|
---|
| 172 | break;
|
---|
| 173 | case '*':
|
---|
| 174 | case 'm':
|
---|
| 175 | accept();
|
---|
| 176 | if(SP > 1){
|
---|
| 177 | hipfloat b = pop();
|
---|
| 178 | hipfloat a = pop();
|
---|
| 179 | push(a.mul(b));
|
---|
| 180 | }
|
---|
| 181 | break;
|
---|
| 182 | case '/':
|
---|
| 183 | case 'd':
|
---|
| 184 | accept();
|
---|
| 185 | if(SP > 1){
|
---|
| 186 | hipfloat b = pop();
|
---|
| 187 | hipfloat a = pop();
|
---|
| 188 | push(a.div(b));
|
---|
| 189 | }
|
---|
| 190 | break;
|
---|
| 191 | case 'x':
|
---|
| 192 | accept();
|
---|
| 193 | if(SP > 1){
|
---|
| 194 | hipfloat a = pop();
|
---|
| 195 | hipfloat b = pop();
|
---|
| 196 | push(a);
|
---|
| 197 | push(b);
|
---|
| 198 | }
|
---|
| 199 | break;
|
---|
| 200 | case 'l':
|
---|
| 201 | accept();
|
---|
| 202 | if(SP > 0){
|
---|
| 203 | push(pop().ln());
|
---|
| 204 | }
|
---|
| 205 | break;
|
---|
| 206 | case 'r':
|
---|
| 207 | accept();
|
---|
| 208 | if(SP > 0){
|
---|
| 209 | push(pop().sqrt());
|
---|
| 210 | }
|
---|
| 211 | break;
|
---|
| 212 | case 'e':
|
---|
| 213 | accept();
|
---|
| 214 | if(SP > 0) {
|
---|
| 215 | push(pop().exp());
|
---|
| 216 | }
|
---|
| 217 | break;
|
---|
| 218 | case 'f':
|
---|
| 219 | accept();
|
---|
| 220 | if(SP > 0) {
|
---|
| 221 | push(pop().floor());
|
---|
| 222 | }
|
---|
| 223 | case 'F':
|
---|
| 224 | accept();
|
---|
| 225 | if(SP > 0) {
|
---|
| 226 | push(pop().ceil());
|
---|
| 227 | }
|
---|
| 228 | break;
|
---|
| 229 | case 'R':
|
---|
| 230 | accept();
|
---|
| 231 | if(SP > 0) {
|
---|
| 232 | push(pop().round());
|
---|
| 233 | }
|
---|
| 234 | break;
|
---|
| 235 | case '!':
|
---|
| 236 | accept();
|
---|
| 237 | if(SP > 0){
|
---|
| 238 | push(pop().factorial());
|
---|
| 239 | }
|
---|
| 240 | break;
|
---|
| 241 | case 'S':
|
---|
| 242 | accept();
|
---|
| 243 | if(SP > 0){
|
---|
| 244 | push(pop().sin());
|
---|
| 245 | }
|
---|
| 246 | break;
|
---|
| 247 | case 'C':
|
---|
| 248 | accept();
|
---|
| 249 | if(SP > 0){
|
---|
| 250 | push(pop().cos());
|
---|
| 251 | }
|
---|
| 252 | break;
|
---|
| 253 | case 'T':
|
---|
| 254 | accept();
|
---|
| 255 | if(SP > 0){
|
---|
| 256 | push(pop().tan());
|
---|
| 257 | }
|
---|
| 258 | break;
|
---|
| 259 | case 'u':
|
---|
| 260 | case '_':
|
---|
| 261 | accept();
|
---|
| 262 | if(SP > 0){
|
---|
| 263 | push(pop().neg());
|
---|
| 264 | }
|
---|
| 265 | break;
|
---|
| 266 | case 13:
|
---|
| 267 | if(EP > 0){
|
---|
| 268 | accept();
|
---|
| 269 | } else {
|
---|
| 270 | if(SP > 0){
|
---|
| 271 | push(top());
|
---|
| 272 | }
|
---|
| 273 | }
|
---|
| 274 | break;
|
---|
| 275 | }
|
---|
| 276 |
|
---|
| 277 | invalidate();
|
---|
| 278 | return true;
|
---|
| 279 | }
|
---|
| 280 |
|
---|
| 281 | void accept() {
|
---|
| 282 | if(EP > 0){
|
---|
| 283 | try {
|
---|
| 284 | push(new hipfloat(new String(ENTRY, 0, EP)));
|
---|
| 285 | } catch (hipfloatError err){
|
---|
| 286 | push(err.actual_return);
|
---|
| 287 | }
|
---|
| 288 | EP = 0;
|
---|
| 289 | dot = 0;
|
---|
| 290 | ee = 0;
|
---|
| 291 | }
|
---|
| 292 | }
|
---|
| 293 |
|
---|
| 294 |
|
---|
| 295 | void push(hipfloat f) {
|
---|
| 296 | STACK[SP] = f;
|
---|
| 297 | SP++;
|
---|
| 298 | }
|
---|
| 299 |
|
---|
| 300 | hipfloat pop() {
|
---|
| 301 | if(SP == 0){
|
---|
| 302 | return null;
|
---|
| 303 | } else {
|
---|
| 304 | SP--;
|
---|
| 305 | hipfloat f = STACK[SP];
|
---|
| 306 | STACK[SP] = null;
|
---|
| 307 | return f;
|
---|
| 308 | }
|
---|
| 309 | }
|
---|
| 310 |
|
---|
| 311 | hipfloat top() {
|
---|
| 312 | if(SP == 0){
|
---|
| 313 | return null;
|
---|
| 314 | } else {
|
---|
| 315 | return STACK[SP-1];
|
---|
| 316 | }
|
---|
| 317 | }
|
---|
| 318 |
|
---|
| 319 | void replace(hipfloat f) {
|
---|
| 320 | STACK[SP-1] = f;
|
---|
| 321 | }
|
---|
| 322 |
|
---|
| 323 | String entry;
|
---|
| 324 |
|
---|
| 325 | hipfloat STACK[] = new hipfloat[128];
|
---|
| 326 | int SP;
|
---|
| 327 |
|
---|
| 328 | char ENTRY[] = new char[64];
|
---|
| 329 | char scratch[] = new char[64];
|
---|
| 330 | int EP;
|
---|
| 331 | int dot;
|
---|
| 332 | int ee;
|
---|
| 333 |
|
---|
| 334 | Font font;
|
---|
| 335 | int height;
|
---|
| 336 |
|
---|
| 337 | ResourceDatabase rdb;
|
---|
| 338 | boolean show_help = true;
|
---|
| 339 | boolean sci_mode = false;
|
---|
| 340 | StaticTextBox help;
|
---|
| 341 |
|
---|
| 342 | String HELP =
|
---|
| 343 | "a>add s>sub m>mul d>div\n"+
|
---|
| 344 | "p>pow e>exp l>ln u>neg\n\n"+
|
---|
| 345 | "S>sin C>cos T>tan r>sqr\n\n"+
|
---|
| 346 | "!>fact f>floor F>ceil\n\n"+
|
---|
| 347 | "x>swap\n\n"+
|
---|
| 348 | "rtn>dup\n"+
|
---|
| 349 | "del>drop\n";
|
---|
| 350 | }
|
---|