Google Glass: how to get a Card to refresh after setting text - refresh

How can I go about refreshing a Card after setting the footnote for it?
Basically, there is a footnote already set for the Card when it is initially created:
private void createCards() {
mCards = new ArrayList<Card>();
Card card;
String[] step;
for (int i = 0; i < steps.size(); i++) {
step = steps.get(i);
card = new Card(this);
card.setText(step[1]);
card.setFootnote("Result:");
mCards.add(card);
}
}
But when I click the menu option Yes or No for that Card, I set the footnote again to show "Result: YES" or "Result: No":
switch (item.getItemId()) {
case 1:
resultYesNo = true;
currentCard.setFootnote("Result: YES");
return true;
case 2:
resultYesNo = false;
currentCard.setFootnote("Result: NO");
return true;
And it does change the footnote, but I have to scroll three cards left or right then go back to the Card to see the change happen.
I want it the Card to refresh right away after setting the footnote the 2nd time.

If you want to update a card, after setting the new footnote, you have to notify the cardScrollAdapter:
switch (item.getItemId()) {
case 1:
resultYesNo = true;
currentCard.setFootnote("Result: YES");
adapter.notifyDataSetChanged(); //your cardScrollAdapter
return true;
case 2:
resultYesNo = false;
currentCard.setFootnote("Result: NO");
adapter.notifyDataSetChanged(); //your cardScrollAdapter
return true;
}
Now, the cards should update.

Related

Clueless about how to use switch

this is my very first post :)
I'm a graphic design student in my second year and we have to make code for business cards.
What I want is when I click with my mouse the background of the business card will change.
It worked with a boolean for two pictures.
But now I want it to work with four images.
Have asked the teacher for help and she sent me information about Switch.
But I have no idea where to put in my code and how to fill it in.
This is my code so far
String Instagram="#Famoys_Saz";
String quote = "Guess my passion ;)";
PFont font;
PImage bg;
PImage Snake2;
PImage snake3;
PImage snake4;
boolean click = true;
void setup() {
size(850, 550);
font = createFont("Galaxyfaceano-4yM9.ttf", 45);
textFont(font);
textAlign(CENTER);
bg = loadImage("Snake.jpg");
Snake2= loadImage ("Snake2.jpg");
click = true;
}
void draw() {
background(bg);
if (click == true ) {
image(bg, 0, 0);
} else {
image(Snake2, 0, 0);
}
text(Instagram, mouseX, mouseY);
text(quote, mouseY, mouseX, 05);
}
void mousePressed() {
click =! click;
}
and this is the code my teacher send me:
if( imageNumber == 1 ) {
} else if ( imageNumber == 2 ) {
} else {
}
Could someone help me out? I'm quite a beginner when it comes to coding.
it will look something like this:
switch( imageNumber ) {
case 1: // same as saying imageNumber == 1
//some code
break;
case 2: // same as saying imageNumber == 2
//some code here
break;
}
switch(imageNumber) {
case 1:
// do stuff
break;
case 2:
// do stuff
break;
case 3:
// do stuff
break;
case 4:
// do stuff
break;
default:
// if no case was selected, do this
}
Switches are easy to read and efficient, and some programmers prefers them to else if. Both are fine, as long as they are readable, but the switch has some unique particularities, like having to use break; between cases. The break; will exit the switch, else it would evaluate every other possible case (thus you're saving some execution time).
It happened to me in a robotic project to deliberately skip the break; statement so the robot would do "everything from this point on", but it's very uncommon and a source of many beginner's bugs to forget the break;.
Depending on the language, switches can evaluate different things, but the classics are integers and strings.
Have fun!

AS3 add if statement to switch?

I'm creating a game in AS3 and currently working on the menu. Don't hate I'm very new to this.
I want this to happen when the case: singlePlayerX. This part starts a new single player game when you press 1 on the keyboard. The problem here is I'm not sure where to insert this part or if that's even possible.
if (Input.keyboard.justPressed(this.evertron_keys.PLAYER_BUTTON_1)) {
Session.application.displayState = new Game();
This is the switch I'm currently working with:
if (Input.keyboard.justPressed(this.evertron_keys.PLAYER_DOWN)) {
for (var i:int = 0; i < this.add_layer.numChildren; i++) {
switch (this.add_layer.getChildAt(i)) {
case this.singlePlayerX:
this.add_layer.removeChild(this.singlePlayerX);
this.add_layer.addChild(this.singlePlayer);
this.add_layer.addChild(this.multiPlayerX);
this.add_layer.removeChild(this.multiPlayer);
break;
case this.multiPlayerX:
this.add_layer.removeChild(this.multiPlayerX);
this.add_layer.addChild(this.multiPlayer);
this.add_layer.addChild(this.highScoreX);
this.add_layer.removeChild(this.highScore);
break;
case this.highScoreX:
this.add_layer.removeChild(this.highScoreX);
this.add_layer.addChild(this.highScore);
this.add_layer.addChild(this.singlePlayerX);
this.add_layer.removeChild(this.singlePlayer);
break;
}
}
}
What this part does is changes what is on the screen when you press button down. So basically what I want is when you press 1 on when case this.singlePlayerX = new Game,
when case this.multiPlayerX = new Multiplayer, and so on.
I have tried putting the new Game if statement in the switch statement but nothing happens when I press 1.
Appreciate the help.

C++ How do I Handle all possible Player Movement inputs?

I am trying to clean up movement code I followed from a video tutorial series that was never finished. My intent is for the character to only ever be able to move on X or Y at any given time (so no diagonal). The character has direction facing to keep in mind.
My issue is the player can still press any key they want, or accidently press two keys at the same time.
Ex. if you move Up and make a right turn, accidentally press Right before letting go of Up.
Or if you press Up, press and let go Right to make a slight movement right while continuing to press Up, the player should continue to move up after letting go of Right without having to re-press Up. etc.
Just to make sure all possible input cases are handled intuitively.
EDIT: This is the code so far and I'm getting weird errors I don't know what's wrong
#pragma once
#include "../game.h"
#include "ECS.h"
#include "Components.h"
#include <list>
using namespace std;
class KeyboardController : public Component
{
public:
TransformComponent *transform;
SpriteComponent *sprite;
std::list<SDL_Event> keyDownList;
SDL_Event lastDirection;
void updateKeyState()
{
if (Game::event.type == SDLK_ESCAPE) {
Game::isRunning = false;
}
else if (Game::event.type == SDL_KEYDOWN) {
keyDownList.push_back(Game::event.key.keysym.sym);
}
else if (Game::event.type == SDL_KEYUP) {
keyDownList.remove(Game::event.key.keysym.sym);
}
}
void init() override
{
transform = &entity->getComponent<TransformComponent>();
sprite = &entity->getComponent<SpriteComponent>();
}
void update() override
{
void updateKeyState();
void updateMovement();
}
};
Severity Code Description Project File Line Suppression State
Error (active) E0304 no instance of overloaded function "std::list<_Ty, _Alloc>::push_back [with _Ty=SDL_Event, _Alloc=std::allocator]" matches the argument list Sandbox C:\file_path\KeyboardController.h 31
Severity Code Description Project File Line Suppression State
Error (active) E0415 no suitable constructor exists to convert from "SDL_Keycode" to "SDL_Event" Sandbox C:\file_path\KeyboardController.h 34
You should basically clean up your code by separating the logic between key events and player movement. So your update() method could look like this:
void update() override
{
updateKeyState();
updateMovement();
}
Since you want the player to move only vertically or horizontally (never diagonally), you have to store the key press order in a data structure that can be easily accessed. I think you could use a doubly-linked list:
std::list<SDL_Event> keyDownList;
and we should also store the last key pressed in order to restore the idle animation of the player:
SDL_Event lastDirection;
The updateKeyState() method should add or remove the key to/from the linked list. We should also check if the player wants to leave the game by pressing ESC:
void updateKeyState() {
if (Game::event.type == SDLK_ESCAPE) {
Game::isRunning = false;
} else if (Game::event.type == SDL_KEYDOWN) {
keyDownList.push_back(Game::event.key.keysym.sym);
} else if (Game::event.type == SDL_KEYUP) {
keyDownList.remove(Game::event.key.keysym.sym);
}
}
The updatePlayerMovement() method is where the magic happens. We should basically check which key was pressed first and update the player position accordingly. We also save the down key in the lastDirection field in order to use it when no key is pressed.
void updateMovement() {
// if any key is down
if (keyDownList.size() > 0) {
const SDL_Event downKey = keyDownList.front();
switch (downKey) {
case SDLK_w:
transform->velocity.y = -1;
transform->velocity.x = 0;
sprite->Play("BackWalk");
lastDirection = downKey;
break;
case SDLK_a:
transform->velocity.x = -1;
transform->velocity.y = 0;
sprite->Play("SideWalk");
sprite->spriteFlip = SDL_FLIP_HORIZONTAL;
lastDirection = downKey;
break;
case SDLK_s:
transform->velocity.y = 1;
transform->velocity.x = 0;
sprite->Play("FrontWalk");
lastDirection = downKey;
break;
case SDLK_d:
transform->velocity.x = 1;
transform->velocity.y = 0;
sprite->Play("SideWalk");
sprite->spriteFlip = SDL_FLIP_NONE;
lastDirection = downKey;
break;
}
} else {
// no key is down, handle idle situations
transform->velocity.x= 0;
transform->velocity.y = 0;
switch (lastDirection) {
case SDLK_w:
sprite->Play("BackIdle");
break;
case SDLK_a:
sprite->Play("SideIdle");
break;
case SDLK_s:
sprite->Play("FrontIdle");
break;
case SDLK_d:
sprite->Play("SideIdle");
break;
}
}
}
Note: I haven't tested this code because I don't have the code and structures from your game. So you may have to edit a piece here and there to make it work for you.

OpenGL - Left mouse click keeps removing my grid

I have created a drawGrid() function that draws a squared grid along my X and Y axis, which works fine. I have then created a menu() function (called in the main()), that toggles the grid on and off, here's the code for that:
void menu(int item)
{
switch (item)
{
case MENU_SWITCH_OFF_GRID:
{
if (gridActive == true)
{
gridActive = true;
}
}
break;
case MENU_SWITCH_ON_GRID:
{
if (gridActive == true)
{
gridActive = false;
}
}
break;
default:
{ /* Nothing */ }
break;
}
glutPostRedisplay();
return;
}
}
The menu switch works fine, as I have created a global variable called gridActive without a true or false value so it doesn't reset each time, that way it can be accessed in my display() function like so:
if (gridActive != true)
{
drawGrid();
gridActive = true;
}
All of this works just fine.
What's my issue?
My issue is, whenever I click the left mouse button, my grid disappears, which I don't want. So I've made a mouse() function like this:
case GLUT_LEFT_BUTTON: if (state == GLUT_DOWN)
{
exit(0); // this has been added to see if
// my program will exit!
}
break;
To test if my program exits when I click the left mouse, and it does.
So instead of using exit(0); what code can i put here so that my grid doesn't disappear when I click the left mouse button? Or is the issue beyond that?
UPDATE:
Here's the mouse function:
void mouse(int button, int state, int x, int y)
{
// these have simply been set-up for me to use
// in the future
switch (button)
{
case GLUT_LEFT_BUTTON: if (state == GLUT_DOWN)
{
}
break;
case GLUT_RIGHT_BUTTON: if (state == GLUT_DOWN)
{
}
break;
default: break;
}
}
Based on your code:
if (gridActive != true)
{
drawGrid();
gridActive = true;
}
You only draw the grid when gridActive is false. However, after every time you draw it, you set gridActive=true which will then stop it from being drawn.
Without more of your code, it's impossible to tell exactly what's going on, but these lines may not be doing what you think they are, and that may be causing some issues.
This never does anything.
if (gridActive == true)
{
gridActive = true;
}
This:
if (gridActive == true)
{
gridActive = false;
}
is the same as:
gridActive = false;
In order to tell what's going on, though, we need to know what happens when you click your mouse button when the exit call isn't there, but you didn't post that code yet.
Also, i don't quite know what you mean by:
I have created a global variable called gridActive without a true or false value so it doesn't reset each time
but it sounds like you made an uninitialized global variable and expect that it has some specific meaning because it's uninitialized?

How to get notification for keydown for CMFCRibbonComboBox?

I have CMFCRibbonComboBox on ribonbar and I want when that user press on a key open droplist and select Item acurding to chars that press by user.
For this purpose I want to get notification for keydown.
How can I to do it?
Thanks
I asked a very similar question on MSDN here and eventually solved it myself with the following hack;
Save a local copy of C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\atlmfc\src\mfc\afxribbonedit.cpp to your project
In BOOL CMFCRibbonRichEditCtrl::PreTranslateMessage(MSG* pMsg) replace this
case VK_DOWN:
if (m_edit.m_bHasDropDownList && !m_edit.IsDroppedDown())
{
m_edit.DropDownList();
return TRUE;
}
with this
case VK_DOWN:
if (m_edit.m_bHasDropDownList && !m_edit.IsDroppedDown())
{
m_edit.DropDownList();
CMFCRibbonBaseElement* pRibbonBaseElement = m_edit.GetDroppedDown();
if (pRibbonBaseElement && (pRibbonBaseElement->IsKindOf(RUNTIME_CLASS(CMFCRibbonComboBox))))
{
CString str;
GetWindowText(str);
CMFCRibbonComboBox *pCombo = (CMFCRibbonComboBox*)pRibbonBaseElement;
int ItemNo = -1;
for (int i = 0; i < pCombo->GetCount(); i++)
{
CString ItemText = pCombo->GetItem(i);
if (ItemText.Left(str.GetLength()).CompareNoCase(str) == 0)
{
ItemNo = i;
break;
}
}
if (ItemNo != -1)
{
pCombo->OnSelectItem(ItemNo);
// Draw and redraw dropdown for selection to show
m_edit.DropDownList();
m_edit.DropDownList();
}
}
return TRUE;
}
For drop lists (as opposed to drop downs) you can similarly hand WM_CHAR to do a first letter search based on the next item after the current position. Note that the above hack would need to be checked against any future updates to the ribbon library and should be dumped once it has been properly implemented in the library.