programming statelistdrawable not working in dialog fragment - android-dialogfragment

I have an custom button, the state will changed when pressed to count down. I want to add the background under state. Here is my Java code:
RoundedColorDrawable bg_enable = new RoundedColorDrawable(0, 0xffe8b655);
bg_enable.setRadii(new float[]{0, 0, r, r, r, r, 0, 0});
bg_enable.setBorder(0xffe8b655, bw);
RoundedColorDrawable bg_disable = new RoundedColorDrawable(0, 0xffbcbcbc);
bg_disable.setRadii(new float[]{0, 0, r, r, r, r, 0, 0});
bg_disable.setBorder(0xffbcbcbc, bw);
StateListDrawable bg_btn = new StateListDrawable();
bg_btn.addState(new int[]{android.R.attr.enabled}, bg_enable);
bg_btn.addState(new int[]{}, bg_disable);
ViewUtils.setBackground(mBtnCode, bg_btn);
But the background is always bg_disable, the button is in android DialogFragment. I don't known why the enabled state not working.

Oh my god, it's my typing issue.
it's android.R.attr.state_enabled not android.R.attr.enabled

Related

ImGui overlay with UpdateLayeredWindow function

As title, I'm trying to create a partially transparent in-game overlay using ImGui that's clickable on the UI but click-through otherwise, i.e. you can click on the ImGUI elements but outside the elements you can interact with the game.
I was able to do it using
https://github.com/ocornut/imgui/blob/master/examples/example_win32_directx11/main.cpp
by making the window style as WS_EX_TOPMOST | WS_EX_LAYERED, and using
SetLayeredWindowAttributes(hwnd, RGB(0, 0, 0), 0, ULW_COLORKEY);
However this would impact the rendering performance of the underlying game.
So, I decided to try the UpdateLayeredWindow function.
Here is one of the templates I referenced :
https://github.com/riley-x/TransparentWindow
This does not impact the performance and worked perfectly, and now I have to integrate ImGUI rendering to replace the green ellipse.
However, ImGUI uses the following code :
g_pd3dDeviceContext->OMSetRenderTargets(1, &g_mainRenderTargetView, NULL);
g_pd3dDeviceContext->ClearRenderTargetView(g_mainRenderTargetView, clear_color_with_alpha);
But in order to use the
BOOL UpdateLayeredWindow(
HWND hWnd,
HDC hdcDst,
POINT *pptDst,
SIZE *psize,
HDC hdcSrc,
POINT *pptSrc,
COLORREF crKey,
BLENDFUNCTION *pblend,
DWORD dwFlags
);
function, I have to tell ImGUI to render to an HDC.
I've thought of some possible solutions,
I'm not sure how g_mainRenderTargetView can possibly bind to an HDC, unlike ID2D1Factory::CreateDCRenderTarget which can actually bind to an HDC with BindDC.
Or I could let the ImGUI render as usual but retrieve the back buffer as a bitmap, and then do the following
HDC hdcWnd = GetDC(hwnd);
HDC hdcMem = CreateCompatibleDC(hdcWnd);
HBITMAP memBitmap = CreateCompatibleBitmap(hdcWnd, rect.right - rect.left, rect.bottom - rect.top);
SelectObject(hdcMem, memBitmap);
m_pRenderTarget->BindDC(hdcMem, &rect);
m_pRenderTarget->BeginDraw();
m_pRenderTarget->Clear({ 0 });
m_pRenderTarget->DrawBitmap(bitmap.Get()); // ???
m_pRenderTarget->EndDraw();
POINT pt0 = { 0 };
SIZE sz = { rect.right - rect.left, rect.bottom - rect.top };
BLENDFUNCTION bfunc = { 0 };
bfunc.AlphaFormat = AC_SRC_ALPHA;
bfunc.BlendFlags = 0;
bfunc.BlendOp = AC_SRC_OVER;
bfunc.SourceConstantAlpha = 255;
UpdateLayeredWindow(hwnd, hdcWnd, &pt0, &sz, hdcMem, &pt0, 0, &bfunc, ULW_COLORKEY);
DeleteObject(memBitmap);
ReleaseDC(hwnd, hdcWnd);
ReleaseDC(0, hdcMem);
where m_pRenderTarget is made from :
D2D1_RENDER_TARGET_PROPERTIES props = D2D1::RenderTargetProperties(
D2D1_RENDER_TARGET_TYPE_DEFAULT,
D2D1::PixelFormat(
DXGI_FORMAT_B8G8R8A8_UNORM,
D2D1_ALPHA_MODE_PREMULTIPLIED),
0,
0,
D2D1_RENDER_TARGET_USAGE_NONE,
D2D1_FEATURE_LEVEL_10
);
d2Factory->CreateDCRenderTarget(&props, m_pRenderTarget.GetAddressOf());
The problem is when I tried to retrieve the backbuffer bitmap, it always failed.
Or maybe there is some other way to make the overlay work without using UpdateLayeredWindow?

GTK ignores any type of window positioning

I've been trying to position a splash screen at the center of the screen. In Windows, such a request is easy using SetWindowPos and a bit of geometric arithmetic. I found out that all requests to move a window are ignored by the windowing manager. So my question is, how come I see so many applications with pretty splash screens properly centered? I started with GTK_WINDOW_TOPLEVEL BTW and just switch to popup trying a few things. Setting the gravity and position do not fail, they are just ignored. Even defined in a .glade file, the window is ignored.
{
GtkWidget *pNewWindow = gtk_window_new(GTK_WINDOW_POPUP);
gtk_window_move(GTK_WINDOW(pNewWindow), 0, 0);
gtk_widget_show_all(pNewWindow);
while (gtk_events_pending())
gtk_main_iteration();
gtk_widget_set_size_request(pNewWindow, width, height);
gtk_window_set_decorated(GTK_WINDOW(pNewWindow), FALSE);
// gtk_window_set_position(GTK_WINDOW(pNewWindow), GTK_WIN_POS_CENTER_ALWAYS);
gtk_window_set_resizable(GTK_WINDOW(pNewWindow), FALSE);
// gtk_window_set_gravity(GTK_WINDOW(pNewWindow), GDK_GRAVITY_CENTER);
gtk_window_move(GTK_WINDOW(pNewWindow), 0, 0);
while (gtk_events_pending())
gtk_main_iteration();
return pNewWindow;
}

ImageJ: how to get Image inside GenericDialog to repaint?

In my ImageJ plugin I display a GenericDialog which has a bunch of images attached to it, like this:
// global:
ColorProcessor cp = new ColorProcessor(50, 50); // new ColorProcessor
ImagePlus ip;
public void run(ImageProcessor ip) {
GenericDialog gdiag = new GenericDialog("Foo"); // new Dialolg
gdiag.addDialogListener(this); // adding Listener
gdiag.addMessage("Lorem Ipsum"); // adding Message
gdiag.addSlider("Bar", 1, 360, 1); // adding Slider
Color c = new Color(r, g, b);
cp.setColor(tarColor);
cp.fill();
ip = new ImagePlus("fooimg", cp);
gdiag.addImage(ip);
gdiag.showDialog();
}
I keep a reference to the Colorprocessor and the ImagePlus. When the slider gets moved on the GenericDialog, my the dialogItemChanged() event fires. Here I change the Color on the Image:
public boolean dialogItemChanged(GenericDialog gd, AWTEvent event) {
float fooVal = (float) ((Scrollbar)(gd.getSliders().get(0))).getValue();
// calculating color based on fooVal ...
Color selColor = new Color(r, g, b);
cp.setColor(selColor);
cp.fill();
}
Now when I run this, the Color in the Image does not update. Only when I change the size of the dialog and move the border over the image, the color displays correctly.
How can I force the dialog to repaint?
I tried so many different updates & repaints, I am out of options.
Wayne Rasband added this capability in the 1.51b12 daily build of ImageJ; see his response on the ImageJ mailing list, where this question was cross-posted.

Allegro5 showing text doesnt work

I have this kind of problem: I want to show the message "Hello!" near the centre of the Allegro screen before the game starts. Don't know why there is always only full window white colour after I compile the program not message "Hello!". I don't know why it doesn't show message "Hello!" . But if I erase the code between comment lines //***** program show the message "Hello!" well. Can someone can tell me how to solve this problem?
#include<allegro5\allegro5.h>
#include<allegro5\allegro_native_dialog.h>
#include<allegro5\allegro_font.h>
#include<allegro5\allegro_ttf.h>
#include<allegro5\allegro_primitives.h>
#include<iostream>
using namespace std;
int main(){
int ScreenWidth = 800, ScreenHeight = 600;
if (!al_init())
{
al_show_native_message_box(NULL, NULL, NULL, "Could not initialize Allegro 5", NULL, NULL);
return -1;
}
al_set_new_display_flags(ALLEGRO_WINDOWED);
ALLEGRO_DISPLAY *display = al_create_display(ScreenWidth, ScreenHeight);//creating window with dimensions: 800:600 px
al_set_window_position(display, 200, 100);//place from left top positioning the frame of display
al_set_window_title(display, "Try to catch me!");
if (!display)//show this message if sth wrong with display
{
al_show_native_message_box(display, "Sample Title", "Display Settings", "Display window was not created succesfully", NULL, ALLEGRO_MESSAGEBOX_ERROR);
return -1;
}
al_init_font_addon();//initialization font addon
al_init_ttf_addon();//initialization ttf(true type font) addon
//INTRO
ALLEGRO_FONT *fontOrbionBlack36 = al_load_font("fonts/Orbitron Black.ttf", 36, NULL);
al_draw_text(fontOrbionBlack36, al_map_rgb(44, 117, 255), ScreenWidth / 2, ScreenHeight / 2, ALLEGRO_ALIGN_CENTRE, "Hello!" );
al_rest(5.0);
//********************************************************************************
al_init_primitives_addon();
al_install_keyboard();
ALLEGRO_COLOR electricBlue = al_map_rgb(44, 117, 255);
ALLEGRO_EVENT_QUEUE *event_queue = al_create_event_queue();
al_register_event_source(event_queue, al_get_keyboard_event_source());
bool done = false;
int x = 10, y = 10;
int moveSpeed = 5;
while (!done)
{
ALLEGRO_EVENT events;
al_wait_for_event(event_queue, &events);
if (events.type = ALLEGRO_EVENT_KEY_DOWN)
{
switch (events.keyboard.keycode)
{
case ALLEGRO_KEY_DOWN:
y += moveSpeed;
break;
case ALLEGRO_KEY_UP:
y -= moveSpeed;
break;
case ALLEGRO_KEY_ESCAPE:
done = true;
break;
}
}
al_draw_rectangle(x, y, x + 20, y + 20, electricBlue, 2.0);
al_flip_display();
al_clear_to_color(al_map_rgb(0, 0, 0));
}
//*****************************************************************************************
al_flip_display();
al_rest(10.0);//rest is very simmilar to Sleep() it is waitg function, waiting 3s then close the allegro display
//al_destroy_font(fontOrbionBlack18);
al_destroy_font(fontOrbionBlack36);
al_destroy_display(display);//destroy the display at the end of the programm
return 0;
}
There are a few problems here that are preventing you from seeing the text.
You've set up a 'wait-for-event then draw' loop that is normally associated with the use of a timer, but you have no timer. This means that unless you trigger some sort of event (e.g. by pressing a keyboard key), al_wait_for_event will stall indefinitely and your program will never reach the call to al_flip_display. You can read more about timers here, but for the time being just know you will have to press a keyboard key to get this program to progress.
The ordering of your clear/draw/flip calls is a bit confusing.
Try something like this within your main loop:
al_clear_to_color(al_map_rgb(0, 0, 0));`
al_draw_rectangle(x, y, x + 20, y + 20, electricBlue, 2.0);
al_draw_text(fontOrbionBlack36, al_map_rgb(44, 117, 255), ScreenWidth / 2, ScreenHeight / 2, ALLEGRO_ALIGN_CENTRE, "Hello!" );
al_flip_display();
In other words, clear first, then draw, then flip. Currently you only draw the text once (before your main loop). This means that clearing the screen will clear the text -- you need to draw the text every frame after clearing the screen but before flipping the display.
If you change those two things, you should be able to see the text after pressing a keyboard button. However, there are a few other changes that might help:
I'd remove those calls to al_rest unless they serve a useful purpose. Currently they just make you wait 5 seconds before you start your main loop (meaning you won't be able to see the text immediately even if you do press a key).
There's no reason to flip the display after your main loop exits, although I guess this was left over from your earlier experiments with showing text.
When checking the event type, use events.type == ALLEGRO_EVENT_KEY_DOWN instead of events.type = ALLEGRO_EVENT_KEY_DOWN. Currently, you're actually assigning the value ALLEGRO_EVENT_KEY_DOWN to events.type, overwriting whatever its original value was. Your compiler may (and probably should) have warned you about this.

unable to draw correctly in Gtk+3

I can draw in a single Gtk.DrawingArea, but when i try to do it for many, for example 50, i got some errors in drawing.
Here is the code you need to check out:
def aggiorna(self, args=()):
import random
import time
while True:
for i in self.indirizzi_ip:
self.r=random.randint(0,10)/10.0
self.g=random.randint(0,10)/10.0
self.b=random.randint(0,10)/10.0
self.cpu_info[i]['drawing_area'].show() #the drawing happens here
time.sleep(1)
def __draw(self, widget, context): #connected to Gtk.DrawingArea.show()
context.set_source_rgb(self.r, self.g, self.b) #random
context.rectangle(0, 0, widget.get_allocated_width(), widget.get_allocated_height())
context.fill()
1) why do i get errors in drawing?
2) why do Gtk.DrawingArea(s) change color ONLY when i update the window (for example i switch from a program to Gtk.DrawingArea window)?
3) why don't i get random colors for each Gtk.DrawingArea?
cant help you on this
because it only changes color when Gtk.DrawingArea repainted itself ("draw" signal)
the r,g,b should be inside "draw" function. you did construct r,g,b but since it's outside the draw function, it did not change while area repainted.
why the sleep?
** edited **
sample code:
....
win = Gtk.Window ()
box = Gtk.Box ()
self.square_list = []
for square in range (10):
self.darea = Gtk.DrawingArea ()
self.set_size_request (50,50)
self.square_list.append (self.darea)
box.add (self.darea)
#for each darea connect to separate "draw" signal
self.darea.connect ("draw", self,_draw)
aggiorna_button = Gtk.Button ('Aggiorna!!!') #sorry i use button
box.add (aggiorna_button)
aggiorna.button.connect ("clicked", self.aggiorna)
def _draw (self, widget, cr):
r = random.randint (0,10)/10.0
g = random.randint (0,10)/10.0
b = random.randint (0,10)/10.0
cr.set_source_rgb (r,g,b)
cr.rectangle (0,0, widget.get_allocated_width(), widget.get_allocated_height())
cr.fill ()
def aggiorna (self, widget):
for darea in self.square_list:
darea.queue_draw()