Rockbox Technical Forums

Support and General Use => Plugins/Viewers => Topic started by: Bilgus on April 15, 2022, 07:16:48 PM

Title: Example plugin
Post by: Bilgus on April 15, 2022, 07:16:48 PM
Code: [Select]
/***************************************************************************
 *             __________               __   ___.
 *   Open      \______   \ ____   ____ |  | _\_ |__   _______  ___
 *   Source     |       _//  _ \_/ ___\|  |/ /| __ \ /  _ \  \/  /
 *   Jukebox    |    |   (  <_> )  \___|    < | \_\ (  <_> > <  <
 *   Firmware   |____|_  /\____/ \___  >__|_ \|___  /\____/__/\_ \
 *                     \/            \/     \/    \/            \/
 * $Id$
 *
 * Copyright (C) 2022
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
 * KIND, either express or implied.
 *
 ****************************************************************************/

#include "plugin.h"
#include "lib/pluginlib_actions.h"

const struct button_mapping* plugin_contexts[]= {
    pla_main_ctx,
#if defined(HAVE_REMOTE_LCD)
    pla_remote_ctx,
#endif
};

int cubeSize = 24;
int cubeSpacing = 4;


#ifdef HAVE_SCROLLWHEEL
#define ACT_UP          PLA_SCROLL_FWD
#define ACT_UP_REPEAT   PLA_SCROLL_FWD_REPEAT
#define ACT_DOWN        PLA_SCROLL_BACK
#define ACT_DOWN_REPEAT PLA_SCROLL_BACK_REPEAT
#else
#define ACT_UP          PLA_UP
#define ACT_UP_REPEAT   PLA_UP_REPEAT
#define ACT_DOWN        PLA_DOWN
#define ACT_DOWN_REPEAT PLA_DOWN_REPEAT
#endif

struct candidate
{
    char character;
    int guess;
};
#define NB_LETTERS 26
struct candidate candidates[NB_LETTERS];
static char guesses[NB_LETTERS];

static void cleanup(void *parameter)
{
    (void)parameter;
}
void make_background(void)
{
    rb->lcd_set_background(LCD_WHITE);
    rb->lcd_set_foreground(LCD_BLACK);
    rb->lcd_clear_display();
}
 
void draw_square(int x, int y, int font_w, int font_h, char letter)
{
    char tempCharacter[2];
    rb->snprintf(tempCharacter, sizeof(tempCharacter), "%c", letter);

    rb->lcd_fillrect(x,y,cubeSize,cubeSize);
    rb->lcd_set_drawmode(DRMODE_COMPLEMENT);

    int x0 = x + (cubeSize - font_w) / 2;
    int y0 = y + (cubeSize - font_h) / 2;
    rb->lcd_putsxy(x0, y0, tempCharacter);

    rb->lcd_set_drawmode(DRMODE_SOLID);
}

 
enum plugin_status plugin_start(const void* parameter)
{
    (void)parameter;
    bool quit = false;
    int action = ACTION_NONE;
    int nbguesses = 0;
 
    for (int i = 0; i < NB_LETTERS; i++)
    {
        candidates[i].character = 65 + i;
        guesses[i] = ' ';
    }
   
    int xStart = (LCD_WIDTH - (5 * cubeSize) - (4 * cubeSpacing)) / 2;
    int yStart = 32;

    rb->lcd_setfont(FONT_SYSFIXED);
    struct font *font = rb->font_get(FONT_SYSFIXED);
    int font_h = font->height;
    int font_w = rb->font_get_width(font, 'W');
   
    int fchr = 0;
   
    while(!quit)
    {
        make_background();
       
        int xPosTest = 0;
        rb->lcd_set_drawmode(DRMODE_COMPLEMENT);
        for (int i = fchr; i < NB_LETTERS; i++)
        {
            char tempCharacter[2];
            rb->snprintf(tempCharacter, sizeof(tempCharacter), "%c", candidates[i].character);
            rb->lcd_putsxy(xPosTest, 0, tempCharacter);
            xPosTest += font_w * 2;
        }
        rb->lcd_drawrect(0, 0, font_w + 2, font_h + 2);
        rb->lcd_set_drawmode(DRMODE_SOLID);

        int xPos = xStart;
        int yPos = yStart;

        for (int i = 0; i < 5; i++)
        {
            for (int j = 0; j < 5; j++)
            {
                char guessed = guesses[i * 5 + j];
                draw_square(xPos, yPos, font_w, font_h, guessed);
                xPos += cubeSize + cubeSpacing;
            }
            yPos += cubeSize + cubeSpacing;
            xPos = xStart;
        }

        //rb->lcd_set_drawmode(DRMODE_BG);
        //rb->lcd_fillrect(0,0,LCD_WIDTH,LCD_HEIGHT);
 
        //rb->lcd_set_drawmode(DRMODE_SOLID);
 
        rb->lcd_update();
 
        action = pluginlib_getaction(0, plugin_contexts, ARRAYLEN(plugin_contexts));
   
        switch (action)
        {
            case PLA_EXIT:
            /* FALL-Through */
            case PLA_CANCEL:
                quit = true;
                cleanup(NULL);
                return PLUGIN_OK;
            case PLA_LEFT:
                break;
            case PLA_RIGHT:
                break;
            case ACT_UP:
            /* FALL-Through */
            case ACT_UP_REPEAT:
                fchr++;
                if (fchr >= NB_LETTERS)
                    fchr = NB_LETTERS -1;
                break;
           
            case ACT_DOWN:
            /* FALL-Through */
            case ACT_DOWN_REPEAT:
                fchr--;
                if (fchr < 0)
                    fchr = 0;
                break;
            case PLA_SELECT:
                guesses[nbguesses++] = candidates[fchr].character;
                if (nbguesses >= NB_LETTERS)
                    nbguesses = 0;
                break;

            default:
                if (rb->default_event_handler_ex(action, cleanup, NULL)
                    == SYS_USB_CONNECTED)
                    return PLUGIN_USB_CONNECTED;
        }

    }
    return PLUGIN_OK;
}