freemyipod r509 - Code Review

Jump to: navigation, search
Repository:freemyipod
Revision:r508‎ | r509 | r510 >
Date:22:52, 3 February 2011
Author:theseven
Status:new
Tags:
Comment:
libUI: Various fixes and new chooser renderer: IconFlow
Modified paths:
  • /libs/ui/SOURCES (modified) (history)
  • /libs/ui/blend.c (modified) (history)
  • /libs/ui/chooser.c (modified) (history)
  • /libs/ui/chooser_action_handler_wheel.c (modified) (history)
  • /libs/ui/chooser_renderer_iconflow.c (added) (history)
  • /libs/ui/chooser_renderer_iconflow.h (added) (history)
  • /libs/ui/export/libui.h (modified) (history)
  • /libs/ui/libui.h (modified) (history)
  • /libs/ui/main.c (modified) (history)

Diff [purge]

Index: libs/ui/export/libui.h
@@ -30,6 +30,7 @@
3131 #include "../chooser.h"
3232 #include "../chooser_action_handler_wheel.h"
3333 #include "../chooser_renderer_list.h"
 34+#include "../chooser_renderer_iconflow.h"
3435
3536
3637 /* increase this every time the api struct changes */
@@ -53,6 +54,7 @@
5455 typeof(chooser_run)* chooser_run;
5556 typeof(chooser_action_handler_wheel)* chooser_action_handler_wheel;
5657 typeof(chooser_renderer_list)* chooser_renderer_list;
 58+ typeof(chooser_renderer_iconflow)* chooser_renderer_iconflow;
5759 };
5860
5961 #endif
Index: libs/ui/SOURCES
@@ -3,3 +3,4 @@
44 chooser.c
55 chooser_action_handler_wheel.c
66 chooser_renderer_list.c
 7+chooser_renderer_iconflow.c
Index: libs/ui/chooser.c
@@ -76,7 +76,8 @@
7777 if (info->actionhandler->init(&data) < 0)
7878 goto done;
7979 data.selected = &info->items[info->defaultitem];
80 - data.position = info->defaultitem * info->actionhandler->stepsperitem(&data);
 80+ int spi = info->actionhandler->stepsperitem(&data);
 81+ data.position = info->defaultitem * spi + spi / 2;
8182 if (info->renderer->init)
8283 if (info->renderer->init(&data) < 0)
8384 goto destroy_actionhandler;
@@ -109,7 +110,7 @@
110111 }
111112 lasttick = USEC_TIMER;
112113 }
113 - redrawneeded = info->renderer->render(&data) == CHOOSER_RESULT_REDRAW;
 114+ if (redrawneeded) redrawneeded = info->renderer->render(&data) == CHOOSER_RESULT_REDRAW;
114115 }
115116 cancel:
116117 data.selected = NULL;
Index: libs/ui/libui.h
@@ -30,6 +30,11 @@
3131 void* addr;
3232 int stride;
3333 };
 34+#define LIBUI_BUFFER(a, b) \
 35+{ \
 36+ .addr = a, \
 37+ .stride = b \
 38+}
3439
3540 struct libui_point
3641 {
@@ -36,6 +41,11 @@
3742 int x;
3843 int y;
3944 };
 45+#define LIBUI_POINT(a, b) \
 46+{ \
 47+ .x = a, \
 48+ .y = b \
 49+}
4050
4151 struct libui_box
4252 {
@@ -42,6 +52,11 @@
4353 struct libui_point pos;
4454 struct libui_point size;
4555 };
 56+#define LIBUI_BOX(a, b) \
 57+{ \
 58+ .pos = a, \
 59+ .size = b \
 60+}
4661
4762 struct libui_location
4863 {
@@ -48,6 +63,11 @@
4964 struct libui_buffer buf;
5065 struct libui_point pos;
5166 };
 67+#define LIBUI_LOCATION(a, b) \
 68+{ \
 69+ .buf = a, \
 70+ .pos = b \
 71+}
5272
5373 struct libui_surface
5474 {
@@ -54,5 +74,10 @@
5575 struct libui_location loc;
5676 struct libui_point size;
5777 };
 78+#define LIBUI_SURFACE(a, b) \
 79+{ \
 80+ .loc = a, \
 81+ .size = b \
 82+}
5883
5984 #endif
Index: libs/ui/chooser_action_handler_wheel.c
@@ -71,7 +71,7 @@
7272 }
7373 case WHEEL_MOVED_ACCEL:
7474 mutex_lock(&data->statemutex, TIMEOUT_BLOCK);
75 - data->position = MIN((data->info->itemcount - 1) * spi, MAX(0, data->position + value));
 75+ data->position = MIN(data->info->itemcount * spi - 1, MAX(0, data->position + value));
7676 data->selected = &data->info->items[data->position / spi];
7777 mutex_unlock(&data->statemutex);
7878 return CHOOSER_RESULT_REDRAW;
Index: libs/ui/blend.c
@@ -117,8 +117,8 @@
118118 while (height--)
119119 {
120120 memcpy(out, in, rowsize);
121 - in += (instride - width) * pixelbytes;
122 - out += (outstride - width) * pixelbytes;
 121+ in += instride * pixelbytes;
 122+ out += outstride * pixelbytes;
123123 }
124124 }
125125
Index: libs/ui/main.c
@@ -32,7 +32,8 @@
3333 .mattecolor = mattecolor,
3434 .chooser_run = chooser_run,
3535 .chooser_action_handler_wheel = &chooser_action_handler_wheel,
36 - .chooser_renderer_list = &chooser_renderer_list
 36+ .chooser_renderer_list = &chooser_renderer_list,
 37+ .chooser_renderer_iconflow = &chooser_renderer_iconflow
3738 };
3839
3940 EMCORE_LIB_HEADER(0x49554365, LIBUI_API_VERSION, LIBUI_MIN_API_VERSION, NULL, NULL, apitable)
Index: libs/ui/chooser_renderer_iconflow.c
@@ -0,0 +1,237 @@
 2+//
 3+//
 4+// Copyright 2011 TheSeven
 5+//
 6+//
 7+// This file is part of emCORE.
 8+//
 9+// emCORE is free software: you can redistribute it and/or
 10+// modify it under the terms of the GNU General Public License as
 11+// published by the Free Software Foundation, either version 2 of the
 12+// License, or (at your option) any later version.
 13+//
 14+// emCORE is distributed in the hope that it will be useful,
 15+// but WITHOUT ANY WARRANTY; without even the implied warranty of
 16+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 17+// See the GNU General Public License for more details.
 18+//
 19+// You should have received a copy of the GNU General Public License along
 20+// with emCORE. If not, see <http://www.gnu.org/licenses/>.
 21+//
 22+//
 23+
 24+
 25+#include "emcorelib.h"
 26+#include "libui.h"
 27+#include "chooser.h"
 28+#include "chooser_renderer_iconflow.h"
 29+
 30+
 31+static int chooser_renderer_iconflow_init(struct chooser_data* data)
 32+{
 33+ const struct chooser_renderer_iconflow_params* params;
 34+ params = (const struct chooser_renderer_iconflow_params*)(data->info->rendererparams);
 35+ if (params->version != CHOOSER_RENDERER_ICONFLOW_PARAMS_VERSION) return -1;
 36+ data->rendererdata = malloc(sizeof(struct chooser_renderer_iconflow_data));
 37+ if (!data->rendererdata) return -2;
 38+ struct chooser_renderer_iconflow_data* rdata;
 39+ rdata = (struct chooser_renderer_iconflow_data*)(data->rendererdata);
 40+ rdata->viewposition = params->startposition * data->info->actionhandler->stepsperitem(data);
 41+ rdata->lastupdate = USEC_TIMER;
 42+ return 0;
 43+}
 44+
 45+static void chooser_renderer_iconflow_geticondata(int x, int y, int w, int h, int siv,
 46+ int dist, const struct libui_surface* icon,
 47+ int* ix, int* iy, int* io)
 48+{
 49+ *ix = x + (w - icon->size.x) * (dist + siv / 2) / siv;
 50+ *iy = y + (h - icon->size.y) - (h - icon->size.y) * dist * dist * 2 / (siv * siv);
 51+ *io = MAX(0, 255 - 510 * ABS(dist) / siv);
 52+}
 53+
 54+static enum chooser_result chooser_renderer_iconflow_render(struct chooser_data* data)
 55+{
 56+ enum chooser_result rc = CHOOSER_RESULT_OK;
 57+ const struct chooser_renderer_iconflow_params* params;
 58+ params = (const struct chooser_renderer_iconflow_params*)(data->info->rendererparams);
 59+ struct chooser_renderer_iconflow_data* rdata;
 60+ rdata = (struct chooser_renderer_iconflow_data*)(data->rendererdata);
 61+ long time = USEC_TIMER;
 62+ long timediff = time - rdata->lastupdate;
 63+ int distance = data->position - rdata->viewposition;
 64+ if (distance) rc = CHOOSER_RESULT_REDRAW;
 65+ int factor = params->smoothness / timediff + 1;
 66+ rdata->accumulator += distance;
 67+ distance = rdata->accumulator / factor;
 68+ rdata->accumulator -= distance * factor;
 69+ rdata->viewposition += distance;
 70+ rdata->lastupdate = time;
 71+ if (params->copy_dest.buf.addr == params->fill_dest.loc.buf.addr
 72+ && params->copy_dest.buf.stride == params->fill_dest.loc.buf.stride
 73+ && params->copy_dest.pos.x == params->fill_dest.loc.pos.x
 74+ && params->copy_dest.pos.y == params->fill_dest.loc.pos.y
 75+ && params->copy_src.size.x == params->fill_dest.size.x
 76+ && params->copy_src.size.y == params->fill_dest.size.y && params->copy_src.loc.buf.addr)
 77+ {
 78+ blendcolor(params->copy_src.size.x, params->copy_src.size.y, params->fill_color,
 79+ params->copy_dest.buf.addr, params->copy_dest.pos.x,
 80+ params->copy_dest.pos.y, params->copy_dest.buf.stride,
 81+ params->copy_src.loc.buf.addr, params->copy_src.loc.pos.x,
 82+ params->copy_src.loc.pos.y, params->copy_src.loc.buf.stride);
 83+ if (params->bg_opacity && params->bg_dest.buf.addr && params->bg_src.loc.buf.addr)
 84+ blend(params->bg_src.size.x, params->bg_src.size.y, params->bg_opacity,
 85+ params->bg_dest.buf.addr, params->bg_dest.pos.x,
 86+ params->bg_dest.pos.y, params->bg_dest.buf.stride,
 87+ params->bg_dest.buf.addr, params->bg_dest.pos.x,
 88+ params->bg_dest.pos.y, params->bg_dest.buf.stride,
 89+ params->bg_src.loc.buf.addr, params->bg_src.loc.pos.x,
 90+ params->bg_src.loc.pos.y, params->bg_src.loc.buf.stride);
 91+ }
 92+ else if (params->copy_dest.buf.stride == params->bg_dest.buf.stride
 93+ && params->bg_src.loc.buf.addr && params->copy_dest.buf.addr == params->bg_dest.buf.addr
 94+ && params->copy_dest.pos.x == params->bg_dest.pos.x && !params->fill_dest.loc.buf.addr
 95+ && params->copy_dest.pos.y == params->bg_dest.pos.y && params->copy_src.loc.buf.addr
 96+ && params->copy_src.size.x == params->bg_src.size.x && params->copy_dest.buf.addr
 97+ && params->copy_src.size.y == params->bg_src.size.y && params->bg_opacity)
 98+ {
 99+ blend(params->copy_src.size.x, params->copy_src.size.y, params->bg_opacity,
 100+ params->copy_dest.buf.addr, params->copy_dest.pos.x,
 101+ params->copy_dest.pos.y, params->copy_dest.buf.stride,
 102+ params->copy_src.loc.buf.addr, params->copy_src.loc.pos.x,
 103+ params->copy_src.loc.pos.y, params->copy_src.loc.buf.stride,
 104+ params->bg_src.loc.buf.addr, params->bg_src.loc.pos.x,
 105+ params->bg_src.loc.pos.y, params->bg_src.loc.buf.stride);
 106+ }
 107+ else
 108+ {
 109+ if (params->copy_src.loc.buf.addr && params->copy_dest.buf.addr)
 110+ blit(params->copy_src.size.x, params->copy_src.size.y, 3,
 111+ params->copy_dest.buf.addr, params->copy_dest.pos.x,
 112+ params->copy_dest.pos.y, params->copy_dest.buf.stride,
 113+ params->copy_src.loc.buf.addr, params->copy_src.loc.pos.x,
 114+ params->copy_src.loc.pos.y, params->copy_src.loc.buf.stride);
 115+ if (params->fill_dest.loc.buf.addr)
 116+ blendcolor(params->fill_dest.size.x, params->fill_dest.size.y, params->fill_color,
 117+ params->fill_dest.loc.buf.addr, params->fill_dest.loc.pos.x,
 118+ params->fill_dest.loc.pos.y, params->fill_dest.loc.buf.stride,
 119+ params->fill_dest.loc.buf.addr, params->fill_dest.loc.pos.x,
 120+ params->fill_dest.loc.pos.y, params->fill_dest.loc.buf.stride);
 121+ if (params->bg_opacity && params->bg_src.loc.buf.addr && params->bg_dest.buf.addr)
 122+ blend(params->bg_src.size.x, params->bg_src.size.y, params->bg_opacity,
 123+ params->bg_dest.buf.addr, params->bg_dest.pos.x,
 124+ params->bg_dest.pos.y, params->bg_dest.buf.stride,
 125+ params->bg_dest.buf.addr, params->bg_dest.pos.x,
 126+ params->bg_dest.pos.y, params->bg_dest.buf.stride,
 127+ params->bg_src.loc.buf.addr, params->bg_src.loc.pos.x,
 128+ params->bg_src.loc.pos.y, params->bg_src.loc.buf.stride);
 129+ }
 130+ void* buf = params->viewport.loc.buf.addr;
 131+ int stride = params->viewport.loc.buf.stride;
 132+ int x = params->viewport.loc.pos.x;
 133+ int y = params->viewport.loc.pos.y;
 134+ int w = params->viewport.size.x;
 135+ int h = params->viewport.size.y;
 136+ int spi = data->info->actionhandler->stepsperitem(data);
 137+ int dir = 1;
 138+ int pos = spi / 2;
 139+ int vpos = rdata->viewposition;
 140+ int iiv = params->iconsinview;
 141+ const struct chooser_renderer_iconflow_itemdata* iparams;
 142+ const struct chooser_item* item = data->info->items;
 143+ while (true)
 144+ {
 145+ if (item == data->selected && dir == 1)
 146+ {
 147+ dir = -1;
 148+ item = &data->info->items[data->info->itemcount - 1];
 149+ pos = spi * data->info->itemcount - spi / 2;
 150+ }
 151+ iparams = (const struct chooser_renderer_iconflow_itemdata*)(item->renderparams);
 152+ const struct libui_surface* icon;
 153+ if (item == data->selected) icon = &iparams->icon_selected;
 154+ else icon = &iparams->icon;
 155+ int ix, iy, io;
 156+ int dist = pos - vpos;
 157+ chooser_renderer_iconflow_geticondata(x, y, w, h, spi * iiv, dist, icon, &ix, &iy, &io);
 158+ if (item == data->selected)
 159+ {
 160+ io = 255;
 161+ if (iparams->text && iparams->text_color)
 162+ rendertext(buf, params->text_pos.x - strlen(iparams->text) * get_font_width() / 2,
 163+ params->text_pos.y, stride, iparams->text_color, 0, iparams->text);
 164+ }
 165+ if (icon->loc.buf.addr && io && ix >= x && iy >= y
 166+ && ix + icon->size.x <= x + w && iy + icon->size.y <= y + h)
 167+ blend(icon->size.x, icon->size.y, io, buf, ix, iy, stride, buf, ix, iy, stride,
 168+ icon->loc.buf.addr, icon->loc.pos.x, icon->loc.pos.y, icon->loc.buf.stride);
 169+ if (item == data->selected) break;
 170+ item += dir;
 171+ pos += dir * spi;
 172+ }
 173+ if (params->preblit && params->preblit(data)) return rc;
 174+ displaylcd(params->blit_dest.x, params->blit_dest.y, params->blit_src.size.x,
 175+ params->blit_src.size.y, params->blit_src.loc.buf.addr, params->blit_src.loc.pos.x,
 176+ params->blit_src.loc.pos.y, params->blit_src.loc.buf.stride);
 177+ if (params->postblit) params->postblit(data);
 178+ return rc;
 179+}
 180+
 181+static const struct chooser_item* chooser_renderer_iconflow_itematpixel(struct chooser_data* data,
 182+ int x, int y)
 183+{
 184+ if (x < 0 || y < 0) return NULL;
 185+ const struct chooser_renderer_iconflow_params* params;
 186+ params = (const struct chooser_renderer_iconflow_params*)(data->info->rendererparams);
 187+ if (x >= params->viewport.size.x || y >= params->viewport.size.y) return NULL;
 188+ struct chooser_renderer_iconflow_data* rdata;
 189+ rdata = (struct chooser_renderer_iconflow_data*)(data->rendererdata);
 190+ const struct chooser_item* result = NULL;
 191+ int vx = params->viewport.loc.pos.x;
 192+ int vy = params->viewport.loc.pos.y;
 193+ int vw = params->viewport.size.x;
 194+ int vh = params->viewport.size.y;
 195+ int spi = data->info->actionhandler->stepsperitem(data);
 196+ int dir = 1;
 197+ int pos = spi / 2;
 198+ int vpos = rdata->viewposition;
 199+ int iiv = params->iconsinview;
 200+ const struct chooser_renderer_iconflow_itemdata* iparams;
 201+ const struct chooser_item* item = data->info->items;
 202+ while (true)
 203+ {
 204+ if (item == data->selected && dir == 1)
 205+ {
 206+ dir = -1;
 207+ item = &data->info->items[data->info->itemcount - 1];
 208+ pos = spi * data->info->itemcount - spi / 2;
 209+ }
 210+ iparams = (const struct chooser_renderer_iconflow_itemdata*)(item->renderparams);
 211+ const struct libui_surface* icon;
 212+ if (item == data->selected) icon = &iparams->icon_selected;
 213+ else icon = &iparams->icon;
 214+ int ix, iy, o;
 215+ int dist = pos - vpos;
 216+ chooser_renderer_iconflow_geticondata(vx, vy, vw, vh, spi * iiv, dist, icon, &ix, &iy, &o);
 217+ if (x >= ix && y >= ix && x < ix + icon->size.x && y < iy + icon->size.y) result = item;
 218+ if (item == data->selected) break;
 219+ item += dir;
 220+ pos += dir * spi;
 221+ }
 222+ return result;
 223+}
 224+
 225+static void chooser_renderer_iconflow_destroy(struct chooser_data* data)
 226+{
 227+ free(data->rendererdata);
 228+}
 229+
 230+
 231+const struct chooser_renderer chooser_renderer_iconflow =
 232+{
 233+ .version = CHOOSER_RENDERER_VERSION,
 234+ .init = chooser_renderer_iconflow_init,
 235+ .render = chooser_renderer_iconflow_render,
 236+ .itematpixel = chooser_renderer_iconflow_itematpixel,
 237+ .destroy = chooser_renderer_iconflow_destroy
 238+};
Index: libs/ui/chooser_renderer_iconflow.h
@@ -0,0 +1,74 @@
 2+//
 3+//
 4+// Copyright 2011 TheSeven
 5+//
 6+//
 7+// This file is part of emCORE.
 8+//
 9+// emCORE is free software: you can redistribute it and/or
 10+// modify it under the terms of the GNU General Public License as
 11+// published by the Free Software Foundation, either version 2 of the
 12+// License, or (at your option) any later version.
 13+//
 14+// emCORE is distributed in the hope that it will be useful,
 15+// but WITHOUT ANY WARRANTY; without even the implied warranty of
 16+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 17+// See the GNU General Public License for more details.
 18+//
 19+// You should have received a copy of the GNU General Public License along
 20+// with emCORE. If not, see <http://www.gnu.org/licenses/>.
 21+//
 22+//
 23+
 24+
 25+#ifndef __CHOOSER_RENDERER_ICONFLOW_H__
 26+#define __CHOOSER_RENDERER_ICONFLOW_H__
 27+
 28+#include "emcorelib.h"
 29+#include "libui.h"
 30+#include "chooser.h"
 31+
 32+
 33+#define CHOOSER_RENDERER_ICONFLOW_PARAMS_VERSION 1
 34+
 35+struct chooser_renderer_iconflow_params
 36+{
 37+ int version;
 38+ struct libui_location copy_dest;
 39+ struct libui_surface copy_src;
 40+ struct libui_location bg_dest;
 41+ struct libui_surface bg_src;
 42+ int bg_opacity;
 43+ struct libui_surface fill_dest;
 44+ uint32_t fill_color;
 45+ struct libui_surface viewport;
 46+ struct libui_point text_pos;
 47+ struct libui_point blit_dest;
 48+ struct libui_surface blit_src;
 49+ uint32_t smoothness;
 50+ uint32_t startposition;
 51+ uint32_t iconsinview;
 52+ bool (*preblit)(struct chooser_data* data);
 53+ void (*postblit)(struct chooser_data* data);
 54+};
 55+
 56+struct chooser_renderer_iconflow_itemdata
 57+{
 58+ struct libui_surface icon;
 59+ struct libui_surface icon_selected;
 60+ char* text;
 61+ uint32_t text_color;
 62+};
 63+
 64+struct chooser_renderer_iconflow_data
 65+{
 66+ int viewposition;
 67+ long lastupdate;
 68+ int accumulator;
 69+};
 70+
 71+
 72+extern const struct chooser_renderer chooser_renderer_iconflow;
 73+
 74+
 75+#endif