aboutsummaryrefslogtreecommitdiff
path: root/src/ui/menuframework
diff options
context:
space:
mode:
Diffstat (limited to 'src/ui/menuframework')
-rw-r--r--src/ui/menuframework/menuclass.cpp115
-rw-r--r--src/ui/menuframework/menuframework.hpp56
2 files changed, 171 insertions, 0 deletions
diff --git a/src/ui/menuframework/menuclass.cpp b/src/ui/menuframework/menuclass.cpp
new file mode 100644
index 0000000..d135a01
--- /dev/null
+++ b/src/ui/menuframework/menuclass.cpp
@@ -0,0 +1,115 @@
+#include "menuframework.hpp"
+#include <cstring>
+#include <cmath>
+SMELT* menuCtrlLCD::sm=NULL;
+SMELT* menuLCD::sm=NULL;
+void menuCtrlLCD::render(float x,float y)
+{
+ fnt->setColor(color);
+ fnt->render(x+skv,y,1,ALIGN_LEFT,NULL,rt);
+}
+void menuCtrlLCD::update()
+{
+ if(overlen)
+ {
+ if(selected)
+ {
+ ++scd;
+ if(scd>30)++scv,scd=0;
+ if(scv>(int)strlen(tr))scv=0;
+ char rr[48];sprintf(rr,"%s %s",tr,tr);
+ sprintf(rt,"%s %.*s",tl,(int)(maxw-strlen(tl)-1),rr+scv);
+ }
+ else
+ {
+ scv=0;
+ sprintf(rt,"%s %.*s...",tl,(int)(maxw-strlen(tl)-4),tr);
+ }
+ }
+ if(skd>0){skv=-skv;--skd;}else skd=skv=0;
+ if(selected)
+ {color=SETA(color,(int)(0x60*fabsf(sin(6*sm->smGetTime())))+0x90);}
+ else
+ color=SETA(color,0x80);
+}
+void menuCtrlLCD::setText(const char* l,const char* r)
+{
+ if(!l)l="";if(!r)r="";
+ tl=l;tr=r;
+ if((int)(strlen(l)+strlen(r)+1)>maxw)
+ {
+ overlen=true;scv=0;scd=0;
+ }
+ else
+ {
+ overlen=false;
+ sprintf(rt,"%s %*s",l,(int)(maxw-strlen(l)-1),r);
+ }
+}
+void menuLCD::addCtrl(menuCtrlLCD *ctrl)
+{
+ if(ctrl->id==0)ctrl->selected=true;else ctrl->selected=false;
+ menuCtrlLCD *last=ctrls;
+ if(!ctrls)
+ {
+ ctrls=ctrl;
+ ctrl->last=0;
+ ctrl->next=0;
+ }
+ else
+ {
+ while(last->next)last=last->next;
+ last->next=ctrl;
+ ctrl->last=last;
+ ctrl->next=0;
+ }
+ ctrl->color=0x80333333;
+}
+menuCtrlLCD* menuLCD::getCtrl(int id)
+{
+ for(menuCtrlLCD* i=ctrls;i;i=i->next)
+ if(i->id==id)return i;
+ return NULL;
+}
+bool menuLCD::update()
+{
+ if(sm->smGetKeyState(SMK_DOWN)==SMKST_HIT)
+ {
+ getCtrl(selected)->selected=false;
+ menuCtrlLCD* c;
+ for(int i=selected+1;(c=getCtrl(i));++i)
+ {if(c->enabled){selected=i;break;}}
+ getCtrl(selected)->selected=true;
+ }
+ if(sm->smGetKeyState(SMK_UP)==SMKST_HIT)
+ {
+ getCtrl(selected)->selected=false;
+ menuCtrlLCD* c;
+ for(int i=selected-1;(c=getCtrl(i));++i)
+ {if(c->enabled){selected=i;break;}}
+ getCtrl(selected)->selected=true;
+ }
+ for(menuCtrlLCD* i=ctrls;i;i=i->next)i->update();
+ if(sm->smGetKey())
+ return getCtrl(selected)->ikp();
+ else return false;
+}
+void menuLCD::render()
+{
+ int cr=rows/2+1;
+ getCtrl(selected)->render(x,y+(cr-1)*itemh);
+ menuCtrlLCD* t=getCtrl(selected);
+ for(int ccr=cr-1;ccr>0;--ccr)
+ {
+ if(t)t=getCtrl(t->id-1);
+ if(!t){fnt->setColor(0x80333333);fnt->printf(x,y+(ccr-1)*itemh,1,ALIGN_LEFT,NULL,"%*s",maxw," ");}
+ else t->render(x,y+(ccr-1)*itemh);
+ }
+ t=getCtrl(selected);
+ for(int ccr=cr+1;ccr<=rows;++ccr)
+ {
+ if(t)t=getCtrl(t->id+1);
+ if(!t){fnt->setColor(0x80333333);fnt->printf(x,y+(ccr-1)*itemh,1,ALIGN_LEFT,NULL,"%*s",maxw," ");}
+ else t->render(x,y+(ccr-1)*itemh);
+ }
+}
diff --git a/src/ui/menuframework/menuframework.hpp b/src/ui/menuframework/menuframework.hpp
new file mode 100644
index 0000000..1879686
--- /dev/null
+++ b/src/ui/menuframework/menuframework.hpp
@@ -0,0 +1,56 @@
+#ifndef BLRMENUFW_H
+#define BLRMENUFW_H
+#include "smelt.hpp"
+#include "smbmfont.hpp"
+typedef bool (*smHooki)(int);
+class menuCtrlLCD
+{
+protected:
+ static SMELT *sm;
+private:
+ smHooki keyPressedf;
+ bool overlen;
+ const char *tl,*tr;
+ int scv,scd,maxw,skv,skd;
+ char rt[64];
+public:
+ int id;
+ bool enabled,selected;
+ DWORD color;
+ menuCtrlLCD *next,*last;
+ smBMFont *fnt;
+ menuCtrlLCD(int _id,int mw,smBMFont *font)
+ {
+ sm=smGetInterface(SMELT_APILEVEL);enabled=true;keyPressedf=NULL;
+ color=0xFFFFFFFF;id=_id;maxw=mw;fnt=font;skv=0;next=last=NULL;
+ }
+ virtual ~menuCtrlLCD(){sm->smRelease();}
+ void render(float x,float y);
+ void update();
+ virtual void updateHook(){}
+ void setText(const char *l,const char *r);
+ void onKeyPressed(smHooki kpf){keyPressedf=kpf;}//parameter is the key pressed. The return value has no effect.
+ void shake(int intv,int dur){skv=intv;skd=dur;}
+ bool ikp(){if(keyPressedf)return keyPressedf(sm->smGetKey());else return false;}
+};
+class menuLCD
+{
+private:
+ static SMELT *sm;
+ float itemh,x,y;
+ int maxw,rows,selected;
+ menuCtrlLCD *ctrls;
+ smBMFont *fnt;
+public:
+ menuLCD(float ih,int mw,int rowc,float _x,float _y,smBMFont *font)
+ {
+ ctrls=NULL;selected=0;sm=smGetInterface(SMELT_APILEVEL);
+ maxw=mw;itemh=ih;rows=rowc;x=_x;y=_y;fnt=font;
+ }
+ ~menuLCD(){menuCtrlLCD *c,*n;c=ctrls;while(c){n=c->next;delete c;c=n;}sm->smRelease();}
+ void addCtrl(menuCtrlLCD *ctrl);
+ menuCtrlLCD* getCtrl(int id);
+ bool update();
+ void render();
+};
+#endif